Conversion of seconds to date
조회 수: 54 (최근 30일)
이전 댓글 표시
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
채택된 답변
James Tursa
2018년 4월 11일
편집: James Tursa
2018년 4월 11일
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
Peter Perkins
2018년 4월 11일
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
추가 답변 (1개)
Valérie Le Guennec
2018년 4월 23일
댓글 수: 1
Ozan Akyildiz
2019년 2월 11일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!