Lost seconds when converting a datetime object with datenum/datestr

5 views (last 30 days)
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 Comments
dpb
dpb on 28 Nov 2016
Edited: dpb on 28 Nov 2016
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
Walter Roberson on 28 Nov 2016
Edited: dpb on 29 Nov 2016
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

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 28 Nov 2016
Edited: Guillaume on 28 Nov 2016
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 Comment
Walter Roberson
Walter Roberson on 29 Nov 2016
"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.)

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!