Convert Julian Date + Time in GMT to calendar day + Time in EST

I am given three strings
year = '2024';
JDDD = 'J025'; % Julian date
GMT = '19:29:29.231' % GMT time
JDDD is the "Julian Date" specifying the number of days elapsed since Jan 1 of the current year. So 025 in JDDD corresponds to January 25th.
I would appreciate if anyone could show me how to convert this data using datetime into the format 'yyyy-MM-dd HH:mm:ss.SSS' with the time in EST.

 Risposta accettata

This works —
syear = '2024';
JDDD = 'J025'; % Julian date
GMT = '19:29:29.231'; % GMT time
doy = sscanf(JDDD,'J%3d')
doy = 25
DTUCT = datetime(str2double(syear),1,doy) + timeofday(datetime(GMT));
DTUCT .TimeZone = 'UCT';
Warning: 'UCT' specifies a time zone with a fixed offset from UTC, +00:00. This zone does not follow daylight saving time, and so may give unexpected results. See the datetime.TimeZone property for details about specifying time zones.
DTUCT.Format = 'yyyy-MMM-dd HH:mm:ss.SSS'
DTUCT = datetime
2024-Jan-25 19:29:29.231
DTEST = DTUCT;
DTEST.TimeZone = 'America/New_York'
DTEST = datetime
2024-Jan-25 14:29:29.231
The datetime function will do the day-of-year conversion automatically. For example, if the day is 250 rather than 25, datetime returns:
Out = datetime(2024,1,250,'TimeZone','GMT') + timeofday(datetime(GMT))
Out = datetime
06-Sep-2024 19:29:29
.

1 Commento

You can do the conversion in one datetime call by using the "D" day of year format specifier and appending the timezone info in the string.
syear = '2024';
JDDD = 'J025'; % Julian date
GMT = '19:29:29.231'; % GMT time
dtStr = "" + syear + "-" + JDDD + " " + GMT + " GMT"
dtStr = "2024-J025 19:29:29.231 GMT"
datetime(dtStr,InputFormat="uuuu-'J'DDD HH:mm:ss.SSS z",TimeZone="America/New_York",Format="uuuu-MMM-dd HH:mm:ss.SSS")
ans = datetime
2024-Jan-25 14:29:29.231

Accedi per commentare.

Più risposte (2)

Note that I have changed the inputs YEAR and JDDD to numeric values as they are easier to work with -
I changed the name to all capital letters as it clashed with the function year. Make sure to not use any inbuilt function names from MATLAB as names for variables/scripts.
%Inputs
YEAR = 2024;
JDDD = 25; % Julian date
GMT = '19:29:29.231'; % GMT time
%Get the date and convert to a char array
d1 = char(datetime(YEAR, 1, JDDD))
d1 = '25-Jan-2024'
%Concatenate the date with time
vec = sprintf('%s %s', d1, GMT)
vec = '25-Jan-2024 19:29:29.231'
%Make a datetime element specifying UTC as the time-zone
out = datetime(vec, 'InputFormat', 'dd-MMM-yyyy HH:mm:ss.SSS', 'TimeZone', 'UTC')
out = datetime
25-Jan-2024 19:29:29
%Convert the time zone to EST
out.TimeZone = 'America/Atikokan';
%Note that there are many different IANA zone IDs for EST timezones -
%America/Atikokan, America/Jamaica and America/Panama etc, Any of these can be used
out
out = datetime
25-Jan-2024 14:29:29

2 Commenti

This works with any julian day from 1st January of the given year -
%Inputs
YEAR = 2024;
JDDD = 69; % Julian date
GMT = '19:29:29.231'; % GMT time
%Get the date and convert to a char array
d1 = char(datetime(YEAR, 1, JDDD))
d1 = '09-Mar-2024'
%Concatenate the date with time
vec = sprintf('%s %s', d1, GMT);
%Make a datetime element specifying UTC as the time-zone
out = datetime(vec, 'InputFormat', 'dd-MMM-yyyy HH:mm:ss.SSS', 'TimeZone', 'UTC');
%Convert the time zone to EST
out.TimeZone = 'America/Atikokan';
out
out = datetime
09-Mar-2024 14:29:29
Even if it is very theoretical, not in 2100 ;-)

Accedi per commentare.

It's not pretty, but it works.
yr = '2024'; %<<< renamed to avoid shadowing the year function
JDDD = 'J025'; % Julian date
GMT = '19:29:29.231' % GMT time
GMT = '19:29:29.231'
t = datetime(str2double(yr), 1, str2double(strrep(JDDD, 'J', '')), ...
str2double(GMT(1:2)), str2double(GMT(4:5)), str2double(GMT(7:end)), 'TimeZone', 'GMT')
t = datetime
25-Jan-2024 19:29:29
t.TimeZone = 'America/New_York'
t = datetime
25-Jan-2024 14:29:29
t.Format = 'yyyy-MM-dd HH:mm:ss.SSS'
t = datetime
2024-01-25 14:29:29.231
Note that this will work even if JDDD is more than 31.
JDDD = 'J035';
t = datetime(str2double(yr), 1, str2double(strrep(JDDD, 'J', '')), ...
str2double(GMT(1:2)), str2double(GMT(4:5)), str2double(GMT(7:end)), 'TimeZone', 'GMT');
t.TimeZone = 'America/New_York';
t.Format = 'yyyy-MM-dd HH:mm:ss.SSS'
t = datetime
2024-02-04 14:29:29.231

Categorie

Prodotti

Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by