Convert to date, hour, and minute
古いコメントを表示
Hi,
We collect data from a data logger that collects date and time stamps in addition to the actual data. Here is the date format.
2011 95 1300
The first and third columns are self explanatory. The 2nd column is the no. of days since January the 1st.
I would like to know if there is a way to convert the information into 4/5/2011 13:00.
Thanks in advance.
採用された回答
その他の回答 (3 件)
Geoff
2012 年 4 月 23 日
Yep,
First, concatenate the first and third columns to use the normal date conversion, then add the second column. Put the result into datestr:
datestr(datenum('20121300', 'yyyyHHMM')+95, 'mm/dd/yyyy HH:MM')
ans =
04/05/2012 13:00
Obviously I've just used constants here, but you get the idea.
You can't get single-character days out of that though - they are padded with zeros.
In that case you'd want to do this:
d = datevec(datenum('20121300', 'yyyyHHMM')+95, 'mm/dd/yyyy HH:MM');
sprintf( '%d/%d/%d %d:%02d', d([2 3 1 4 5]) )
ans =
4/5/2012 13:00
3 件のコメント
Bahram
2012 年 4 月 23 日
Walter Roberson
2012 年 4 月 23 日
Do not quote str in the datenum() call: it is a variable rather than a literal.
Bahram
2012 年 4 月 23 日
Walter Roberson
2012 年 4 月 23 日
Try
datestr([M(:,1), ones(size(M,1),1), M(:,2)+1, round(M(:,3)./100), mod(M(:,3),100), zeros(size(M,1),1)], 'm/d/yyyy HH:MM')
This assumes that "days since January the 1st" will be 0 for Jan 1.
The part in [] constructs dates in the medium-length datevec format (year month day hours minutes seconds). The adjustment to convert between day number and proper month/day is handled by specifying (e.g.) Jan 95th and letting the date calculation routines do the fixup.
2 件のコメント
Geoff
2012 年 4 月 23 日
That call to 'round' should be 'floor' or it will give incorrect results for the last 10 minutes of each hour. Unfortunately a single 'm' and 'd' in calls to 'datestr' expand to the capitalised first letter of the month and day respectively.
Walter Roberson
2012 年 4 月 23 日
Ah yes, I was worried about floating point round-off when I used round() and didn't think it through.
per isakson
2012 年 4 月 23 日
I run this. Matlab seems to more clever than I anticipated. Is this documented behavior?
str = '2011 95 1300'
datestr( datenum( str, 'yyyy dd HHMM' ), 31 )
str = '2012 95 1300'
datestr( datenum( str, 'yyyy dd HHMM' ), 31 )
str =
2011 95 1300
ans =
2011-04-05 13:00:00
str =
2012 95 1300
ans =
2012-04-04 13:00:00
2 件のコメント
Oleg Komarov
2012 年 4 月 23 日
Expected and documented behavior.
per isakson
2012 年 4 月 23 日
I cannot find that documentation!
カテゴリ
ヘルプ センター および File Exchange で Calendar についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!