Lost seconds when converting a datetime object with datenum/datestr

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
dpb 2016 年 11 月 28 日
Which release? I don't see that here with or without explicit format string...
>> datestr(datenum('01.07.2016 08:57:50','mm.dd.yyyy HH:MM:SS'))
ans =
07-Jan-2016 08:57:50
>> datestr(datenum('01.07.2016 08:57:50'))
ans =
01-Jan-2016 08:57:50
>>
>> version
ans =
8.0.0.783 (R2012b)
>>
Walter Roberson
Walter Roberson 2016 年 11 月 28 日
The user's original array is a datetime object which did not exist in your release, dpb
dpb
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
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
Guillaume 2016 年 11 月 28 日
編集済み: Guillaume 2016 年 11 月 28 日

0 投票

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
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.)

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

カテゴリ

ヘルプ センター および File ExchangeDates and Time についてさらに検索

質問済み:

2016 年 11 月 28 日

コメント済み:

2016 年 11 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by