How to convert serial number to date number

40 visualizaciones (últimos 30 días)
A. A. L
A. A. L el 12 de Jul. de 2023
Editada: Stephen23 el 18 de Jul. de 2023

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 comentarios
Pranavkumar Mallela
Pranavkumar Mallela el 12 de Jul. de 2023
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
Stephen23 el 12 de Jul. de 2023
Editada: Stephen23 el 13 de Jul. de 2023
"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.

Iniciar sesión para comentar.

Respuesta aceptada

Aditya Singh
Aditya Singh el 12 de Jul. de 2023
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 comentarios
Stephen23
Stephen23 el 12 de Jul. de 2023
"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
Peter Perkins el 17 de Jul. de 2023
Editada: Peter Perkins el 17 de Jul. de 2023
daysSince1950 = [21123,23010];
dn1950 = datenum(datetime(1950,1,0))
dn1950 = 712223
dt = datetime(daysSince1950+dn1950, 'ConvertFrom', 'datenum')
dt = 1×2 datetime array
31-Oct-2007 30-Dec-2012
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])
ans = 1×2 datetime array
30-Oct-0057 30-Dec-0062
datetime(1950,1,0) + caldays([21123,23010])
ans = 1×2 datetime array
31-Oct-2007 30-Dec-2012
between(datetime(1950,1,0),datetime([2007 2012],[10 12],[31 30]),"days")
ans = 1×2 calendarDuration array
21123d 23010d
If these numbers are literally "days since 1-Jan-1950, starting at 1", then this
dt = datetime([21123,23010], 'ConvertFrom', 'datenum')
dt = 1×2 datetime array
30-Oct-0057 30-Dec-0062
is "correct", but then this
dt.Year = dt.Year + 1950 % what SS did, but more directly
dt = 1×2 datetime array
30-Oct-2007 30-Dec-2012
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.

Iniciar sesión para comentar.

Más respuestas (1)

Stephen23
Stephen23 el 17 de Jul. de 2023
Editada: Stephen23 el 18 de Jul. de 2023
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)
D = 1×2 datetime array
2007-11-01 00:00:00 2012-12-31 00:00:00
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)
D = datetime
2020-12-31 00:00:00
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 comentario
Peter Perkins
Peter Perkins el 17 de Jul. de 2023
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.

Iniciar sesión para comentar.

Categorías

Más información sobre Calendar en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by