How can i generate month and year strings for Datetime vectors?

30 ビュー (過去 30 日間)
Steven Niggebrugge
Steven Niggebrugge 2016 年 6 月 19 日
コメント済み: dpb 2017 年 12 月 16 日
How can i generate month and year concatenated strings for Datetime vectors?
E.g. i have vector ['01/Jan/2015';'01/Feb/2015'], how can i transform this into [201501; 201502] ?

回答 (4 件)

Star Strider
Star Strider 2016 年 6 月 20 日
This works:
DTV = datetime(['01/Jan/2015';'01/Feb/2015']); % Create ‘datetime’ Object
Out = fix(yyyymmdd(DTV)/100) % Desired Output
Out =
201501
201502
  3 件のコメント
Star Strider
Star Strider 2016 年 6 月 20 日
‘Ours is not to reason why ...’
dpb
dpb 2016 年 6 月 20 日
編集済み: dpb 2016 年 6 月 20 日
Man, what that must do to the JIT optimizer...no wonder the newer versions suffer in size/performance to incorporate such...
I've not found this documented; I did discover there's
dateType — Type of values in X
'datenum' | 'excel' | 'juliandate' | 'posixtime' | 'yyyymmdd' | ...
which would let the above fix() operation work w/o the reference if used the 'yyyymmdd' output type when creating the object. It doesn't say default there altho presume it would be 'datenum'???
ADDENDUM/ERRATUM
OK, I did a search and indeed TMW did actually write and include a function yyyymmdd -- who'd've'a thunk of such an animuhl...but, anyway, it's not so magical after all; "there's nothing to see here, please move on" just a momentary "huh?!" as hadn't seen the function reference and it's just bizzare enough I didn't think of it as an ordinary function name.

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


dpb
dpb 2016 年 6 月 19 日
編集済み: dpb 2016 年 6 月 20 日
t = datetime(DateStrings,'InputFormat','dd/MMM/yyyy', ...
'Format','yyyyMM');
should do it (altho I don't have recent release to test...
OK, to actually convert to numerics,
str2num(datestr(datenum(['01/Jan/2015';'01/Feb/2015']),'YYYYmm'))
  2 件のコメント
Steven Niggebrugge
Steven Niggebrugge 2016 年 6 月 20 日
Hi, thanks for this, but it won't do the trick. This will keep the format as DateTime, it only present the value in a different way (yyyyMM instead of dd-MM-yyyy). I was looking for a conversion into a string or number variable, with value (e.g.) '201502', so no DateTime format. Any ideas on this?
dpb
dpb 2017 年 12 月 16 日
While surely have solved the problem long ere now, a more recent comment brought the thread to attention--
The above is precisely what S Strider's answer does...

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


Shameer Parmar
Shameer Parmar 2016 年 6 月 20 日
Hello Steven,
here is the code..
dateArray = ['01/Jan/2015';'01/Feb/2015'];
for i=1:size(dateArray,1)
date = dateArray(i,:);
dateSplit = regexp(date,'/','split');
switch dateSplit{2}
case 'Jan'
month = '01';
case 'Feb'
month = '02';
case 'Mar'
month = '03';
.
.
.
.
case 'Dec'
month = '12';
end
newDate(i,:) = [dateSplit{3},month];
end
  2 件のコメント
Ayodele Ogunkoya
Ayodele Ogunkoya 2017 年 12 月 7 日
Hello, I am new to matlab. I will like to create a monhtly loop. For example i will like to loop through each days of each months of the year so that i can save output of each month of the year
dpb
dpb 2017 年 12 月 16 日
Better form would be to create a new question with specifics for you precise situation rather than bury a comment in an old thread expecting an answer.
If you have an array of datetime values, simply iterate. If that's not the problem/question, see above comment... :)

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


Peter Perkins
Peter Perkins 2016 年 8 月 3 日
To create numbers use the Year and Month properties of a datetime
>> d = datetime+calmonths([1;2;3])
d =
03-Sep-2016 16:37:49
03-Oct-2016 16:37:49
03-Nov-2016 16:37:49
>> 100*d.Year + d.Month
ans =
201609
201610
201611
or use SS's suggestion:
>> fix(yyyymmdd(d)/100)
ans =
201609
201610
201611
To create strings you could just call num2str on that, or ...
>> cellstr(d,'yyyyMM')
ans =
'201609'
'201610'
'201611'

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by