Lost seconds when converting a datetime object with datenum/datestr

조회 수: 18 (최근 30일)
Pierre Queloz
Pierre Queloz 2016년 11월 28일
댓글: Walter Roberson 2016년 11월 29일
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일
편집: 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일
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.)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by