How do I change how the date is written when I use datestr?
It displays 29-Sep-2014 but I want it to say 29.september year 2014
?

 採用された回答

José-Luis
José-Luis 2014 年 9 月 29 日
編集済み: José-Luis 2014 年 9 月 29 日

0 投票

vec = datevec(now);
month_list = {'January' 'February' 'March' 'April' 'May' 'June' ...
'July' 'August' 'September' 'October' 'November' 'December'};
your_string = sprintf('%i.%s year %i',vec(3),month_list{vec(2)}, vec(1))
Please accept the answer that best solves your problem.

その他の回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 9 月 29 日

1 投票

strrep(datestr(now,'dd.mmmm _ yyyy'),'_','year')

4 件のコメント

José-Luis
José-Luis 2014 年 9 月 29 日
編集済み: José-Luis 2014 年 9 月 29 日
month_list = {'January' 'February' 'March' 'April' 'May' 'June' ...
'July' 'August' 'September' 'October' 'November' 'December'};
tic
vec = datevec(now);
your_string = sprintf('%i.%s year %i',vec(3),month_list{vec(2)}, vec(1));
toc
tic
strrep(datestr(now,'dd.mmmm _ yyyy'),'_','year');
toc
Elapsed time is 0.000384 seconds.
Elapsed time is 0.091557 seconds.
I just felt the need to pointlessly discuss this.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 9 月 29 日
You can't make a comparison like that. With your code I found
Elapsed time is 0.013061 seconds.
Elapsed time is 0.003681 seconds.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 9 月 29 日
Using a for loop
month_list = {'January' 'February' 'March' 'April' 'May' 'June' ...
'July' 'August' 'September' 'October' 'November' 'December'};
tic
for k=1:10000
vec = datevec(now);
your_string = sprintf('%i.%s year %i',vec(3),month_list{vec(2)}, vec(1));
end
toc
tic
for k=1:10000
strrep(datestr(now,'dd.mmmm _ yyyy'),'_','year');
end
toc
Elapsed time is 0.788404 seconds.
Elapsed time is 2.663671 seconds.
José-Luis
José-Luis 2014 年 9 月 29 日
I don't see why the comparison should be invalid. It is faster even if I include the cell array generation in the computation time:
tic
month_list = {'January' 'February' 'March' 'April' 'May' 'June' ...
'July' 'August' 'September' 'October' 'November' 'December'};
vec = datevec(now);
your_string = sprintf('%i.%s year %i',vec(3),month_list{vec(2)}, vec(1));
toc
tic
strrep(datestr(now,'dd.mmmm _ yyyy'),'_','year');
toc
Elapsed time is 0.000576 seconds.
Elapsed time is 0.001291 seconds.

サインインしてコメントする。

Thorsten
Thorsten 2014 年 10 月 1 日

0 投票

[datestr(now, 'dd.mmmm') ' year ' datestr(now, 'yyyy')]

タグ

質問済み:

2014 年 9 月 29 日

回答済み:

2014 年 10 月 1 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by