Lost seconds when converting a datetime object with datenum/datestr

17 vues (au cours des 30 derniers jours)
Pierre Queloz
Pierre Queloz le 28 Nov 2016
Commenté : Walter Roberson le 29 Nov 2016
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 commentaires
dpb
dpb le 28 Nov 2016
Modifié(e) : dpb le 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 le 28 Nov 2016
Modifié(e) : dpb le 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

Connectez-vous pour commenter.

Réponses (1)

Guillaume
Guillaume le 28 Nov 2016
Modifié(e) : Guillaume le 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 commentaire
Walter Roberson
Walter Roberson le 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.)

Connectez-vous pour commenter.

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by