How to convert serial number to date number
조회 수: 36 (최근 30일)
이전 댓글 표시
I have this serial number 21123:23010 that I want to convert into date number 'DD MM YYYY'.
I have tried this cod but didn't work properly. It appears as a text not in columm
T=datestr(21123:23010,'yyyy-mm')
댓글 수: 2
Pranavkumar Mallela
2023년 7월 12일
Hi, can you elaborate on what the desired result is? The code you provided seems to be returning a 1888 x 10 'char' matrix. Thanks.
Stephen23
2023년 7월 12일
편집: Stephen23
2023년 7월 13일
"I have this serial number 21123:23010 "
Those are not MATLAB serial date numbers (unless you are working with years 57AD to 62AD... which is very unlikely).
It might be some other kind of serial date number (if so you need to give us the unit and epoch).
It looks like it might be some integers whose digits just happen to correspond to the digits of some dates using completely different units. For example, a 2-digit year and 3-digit day of the year.
In any case, the best step is to save your data in a MAT file and upload it here by clicking the paperclip button. And give us the exact definition of how those values encode the dates.
채택된 답변
Aditya Singh
2023년 7월 12일
Hi,
To my understanding you are trying to convert the serial number to dates of the format DD MM YYYY. The following code will help.
T = datetime(21123:23010, 'ConvertFrom', 'datenum');
output = datestr(T, 'dd mm yyyy')
str = string(output);
% To convert from char to cell format
output = splitlines(str)
output = cellstr(output);
output = join(output, '\n');
class(output)
For more information, please refer to
Hope it helps!
댓글 수: 6
Stephen23
2023년 7월 12일
"I downloaded the data from the COPERNICUS MARINE ENVIRONMENT MONITORING SERVICE (CMEMS) for the years 2007 - 2012. So this 57 should correspond to 2007 and the 62 to 2012."
So you just assumed that CMEMS uses MATLAB's deprecated serial date numbers and then accepted the first answer on this thread that provided some nonsense conversion?
"I don't know how to move forward."
Read the specification of the data format that you have downloaded, inform us exactly what date encoding it uses.
Peter Perkins
2023년 7월 17일
편집: Peter Perkins
2023년 7월 17일
daysSince1950 = [21123,23010];
dn1950 = datenum(datetime(1950,1,0))
dt = datetime(daysSince1950+dn1950, 'ConvertFrom', 'datenum')
1950 is not a leap year and 0 is! So I'm pretty sure that 30-Oct-2007 is one day off.
datetime(0,1,0) + caldays([21123,23010])
datetime(1950,1,0) + caldays([21123,23010])
between(datetime(1950,1,0),datetime([2007 2012],[10 12],[31 30]),"days")
If these numbers are literally "days since 1-Jan-1950, starting at 1", then this
dt = datetime([21123,23010], 'ConvertFrom', 'datenum')
is "correct", but then this
dt.Year = dt.Year + 1950 % what SS did, but more directly
preserves the day/month, and so fails to account for 21123 days from datenum's origin being 30-Oct while 21123 days from 1950's origin is 31-Oct. I was gonna say that the fault lies in datenum, but actually you'd get that same mistake if you used the same arithmetic for datetime. (Still, stay away from datenums!) 2012 is a leap year, so the errors cancel out for that one.
This assumes ""days since 1-Jan-1950, starting at 1"", which I have no idea if it's right or not.
추가 답변 (1개)
Stephen23
2023년 7월 17일
편집: Stephen23
2023년 7월 18일
The CMEMS documentation gives several possible time units, one of them is
% julian_day_unit = "days since 1950-01-01 00:00:00" ;
V = [21123,23010];
D = datetime(1950,1,1,'Format','u-MM-dd HH:mm:ss') + caldays(V)
This makes it clear that you downloaded data for Nov 2007 through to Dec 2012.
We can check this conversion using the adjacent information fields given in the documentation, which conveniently gives one date both in calendar and "julian" formats. This makes is easy to confirm this method:
% field_date = "2020-12-31 00:00:00" ;
% field_julian_date = 25932.f ;
D = datetime(1950,1,1,'Format','u-MM-dd HH:mm:ss') + caldays(25932)
In contrast any code that returns the years 57AD and 62 AD is complete nonsense. The dates 30th of Oct/Dec are also wrong.
댓글 수: 1
Peter Perkins
2023년 7월 17일
This is consistent with my other comment, except for the 1-based vs. 0-based counting convention. Dunno which is right, I chose 1-based, but you're probably right.
참고 항목
카테고리
Help Center 및 File Exchange에서 Calendar에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!