Conversion of seconds to date
51 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Valérie Le Guennec
am 11 Apr. 2018
Kommentiert: Ozan Akyildiz
am 11 Feb. 2019
Hello,
I have a datasets with time expressed in seconds since 1950-01-01 00:00:00. The two times I would like to convert are : [1907798400] and [1908230400]. I would like to convert them in a date format (Yr-Mnth-Day Hr:min:sec) I previously look on the forum to help me and I did the following steps :
base = datenum(1950,1,1);
datestr([1907798400]/86400) + base)
>> '16-Jun-2010'
base = datenum(1950,1,1);
datestr([1908230400]/86400) + base)
>> '21-Jun-2010'
However, I expect to obtain dates in the months of July 2010. Also, when I open the datasets using Panoply, I obtain the following dates : 2010-07-01 00:00:00 - 2010-07-06 00:00:00. (Yr-Mnth-Day Time)
This is more likely to be what I expect. However, I don't understand why using Matlab I obtain different dates? The year is correct (2010), but not the month. Is there something I am doing wrong?
Could you help me solve this issue?
Thank you a lot for your help.
Regards
0 Kommentare
Akzeptierte Antwort
James Tursa
am 11 Apr. 2018
Bearbeitet: James Tursa
am 11 Apr. 2018
MATLAB is giving you the result of June 16, 2010 because that is the correct answer for the input of 1907798400. This matches intuition as well, e.g. a crude calculation:
>> (1907798400/86400)/365.25
ans =
60.4545
So you would expect the answer to be 60 years and slightly less than 1/2 year later than the base, which we could easily expect to be in June 2010.
The datetime( ) function also agrees with this:
>> datetime(1950,1,1) + seconds(1907798400)
ans =
datetime
16-Jun-2010 00:00:00
or
>> datetime(1950,1,1,0,0,1907798400)
ans =
datetime
16-Jun-2010 00:00:00
Are you sure you are using the same inputs when you compare the MATLAB results to the Panoply results?
1 Kommentar
Peter Perkins
am 11 Apr. 2018
Also:
>> d = datetime([1907798400 1908230400],'ConvertFrom','epochtime','Epoch','01-Jan-1950 00:00:00')
d =
1×2 datetime array
16-Jun-2010 00:00:00 21-Jun-2010 00:00:00
Weitere Antworten (1)
Valérie Le Guennec
am 23 Apr. 2018
1 Kommentar
Ozan Akyildiz
am 11 Feb. 2019
Why is this the accepted answer? This is not helping the reader. I suggest Posting the answer you found as an "Answer" then selecting it.
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!