Lost seconds when converting a datetime object with datenum/datestr
17 次查看(过去 30 天)
显示 更早的评论
As the code below shows, I'm not able to keep track of the seconds when using the datetime/datestr functions on a datetime array:
a =
01.07.2016 08:57:50
01.07.2016 08:58:50
01.07.2016 08:59:50
>> datenum(a)
ans =
1.0e+05 *
7.3651
7.3651
7.3651
>> datestr(a)
ans =
01-Jul-2016 08:57:00
01-Jul-2016 08:58:00
01-Jul-2016 08:59:00
>> datenum(a)
ans =
1.0e+05 *
7.3651
7.3651
7.3651
>> datestr(datenum(a))
ans =
01-Jul-2016 08:57:00
01-Jul-2016 08:58:00
01-Jul-2016 08:59:00
Why is it so? Thanks for your help.
4 个评论
dpb
2016-11-28
编辑:dpb
2016-11-28
Ah...the difference in representations bites, huh? :( Makes one wonder why OP applied datenum to it then??? Or, more significantly that Matlab hasn't updated datenum to provide warning...
As aside, datetime is one enhancement wouldn't mind having; not sure where to go with limited hardware, however; don't think have the resources to run later versions but guess something's going to have to give--the current license expires in about a month... :(
Walter Roberson
2016-11-28
编辑:dpb
2016-11-29
Which release are you using? This does not happen to me in R2016b.
a = datetime('01.07.2016 08:57:50', ...
'InputFormat', 'dd.MM.yyyy hh:mm:ss', ...
'format', 'dd.MM.yyyy hh:mm:ss') + minutes(0:2).'
datestr(a)
ans =
01-Jul-2016 08:57:50
01-Jul-2016 08:58:50
01-Jul-2016 08:59:50
回答(1 个)
Guillaume
2016-11-28
编辑:Guillaume
2016-11-28
Same as dpb, I don't see this behaviour. In R2016b,
>> a = datetime(2016, 7, 1, 8, [57;58;59], 50, 'Format', 'dd.MM.yyyy hh:mm:ss')
a =
3×1 datetime array
01.07.2016 08:57:50
01.07.2016 08:58:50
01.07.2016 08:59:50
>> format longg %default short format does not display enough significant digits
>> datenum(a)
ans =
736512.37349537
736512.374189815
736512.374884259
>> datestr(a)
ans =
01-Jul-2016 08:57:50
01-Jul-2016 08:58:50
01-Jul-2016 08:59:50
More importantly, why do you want to convert to datestr or datenum? The datetime format is more flexible and more practical for date and time calculations.
If you want to convert the datetime array to strings, use char (or since R2016, string)
>> carray = char(a)
carray =
01.07.2016 08:57:50
01.07.2016 08:58:50
01.07.2016 08:59:50
>> sarray = string(a)
sarray =
3×1 string array
"01.07.2016 08:57:50"
"01.07.2016 08:58:50"
"01.07.2016 08:59:50"
1 个评论
Walter Roberson
2016-11-29
"The datetime format is more flexible and more practical for date and time calculations"
Not always.
t1 = datetime('01-Jan-2016')
t2 = datetime('08:57:50')
t1 + t2
is an error.
t2 - dateshift(t2,'start','day') + t1
is too obscure and people are too prone to subtract datetime('today') from t2 instead of shifting to start of day. Subtracting datetime('today') would fail if you crossed midnight between the time that t2 was created and the time you used datetime('today')
This matters because readtable() automatically converts
01-Jan-2016 08:57:50
into two datetime objects because it treats the space as ending the object. (readtable() delegates to textscan() and textscan() has this limitation on %D objects.)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!