How to do math using datetime objects including years
10 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I want to compute the number of days, minutes and seconds since January 1st, 2000 (Julian days) up to a specific date such as June 27, 2022. The "duration" function only allows input formats such as 'dd:hh:mm:ss', but not years. Is there some way to work around this?
date_obs = '22:179:18:42:44.610';
j2000date = '2000:001:12:00:00.000';
formatOut = 'uu:DDD:HH:mm:ss.SSS'; % 22:179:18:42:44.610
j2000format = 'uuuu:DDD:HH:mm:ss.SSS'; % 2000:001:12:00:00.000
Date = duration(date_obs, 'InputFormat', formatOut);
Date2000 = duration(j2000date, 'InputFormat', j2000format);
daysSinceEpoch = Date - Date2000;
Error using duration
Unsupported format 'uu:DDD:HH:mm:ss.SSS'.
0 Commenti
Risposta accettata
Kevin Holly
il 2 Dic 2022
date_obs = '22:179:18:42:44.610';
j2000date = '2000:001:12:00:00.000';
formatOut = 'uu:DDD:HH:mm:ss.SSS'; % 22:179:18:42:44.610
j2000format = 'uuuu:DDD:HH:mm:ss.SSS'; % 2000:001:12:00:00.000
Date = datetime(date_obs, 'InputFormat', formatOut)
Date2000 = datetime(j2000date, 'InputFormat', j2000format)
daysSinceEpoch = between(Date,Date2000)
2 Commenti
Steven Lord
il 2 Dic 2022
You don't. In order to do that you would need to know how long 1 calendar month is and you can't answer that in isolation. Does that calendar month represent January (31 days) or February (either 28 or 29 days)?
Più risposte (2)
Steven Lord
il 2 Dic 2022
date_obs = '22:179:18:42:44.610';
j2000date = '2000:001:12:00:00.000';
These don't look like durations, they look like datetimes. That 12 in your j2000date looks a little suspicious too; did you really mean noon or did you want midnight?
formatOut = 'uu:DDD:HH:mm:ss.SSS'; % 22:179:18:42:44.610
j2000format = 'uuuu:DDD:HH:mm:ss.SSS'; % 2000:001:12:00:00.000
Date = datetime(date_obs, 'InputFormat', formatOut)
You probably want to add 2000 calendar years here.
Date = Date + calyears(2000)
Or you could split your string into its components then create the datetime array from the resulting vector.
v = double(split(string(date_obs), ':'));
Date = datetime(v(1)+2000, 1, 0) + days(v(2)) + duration(v(3:5).')
Date2000 = datetime(j2000date, 'InputFormat', j2000format)
Now subtract.
timeSinceEpoch = Date - Date2000
format longg
daysSinceEpoch = days(timeSinceEpoch)
Is this result reasonable? Roughly estimating the difference between those two dates is 22 and a half years. What's that in days?
check1 = days(years(22.5)) % or
check2 = 22.5 * 365 % Units: years * days/year = days
Rather than doing plain subtraction you could use between if you want the difference as a calendarDuration rather than a duration.
d = between(Date2000, Date)
4 Commenti
Seth Furman
il 8 Dic 2022
According to the Python code this was derived from, the interval should be 8213.94566 days.
Could you provide the equivalent Python code you used to get 8213.94566? I'd like to make sure I understand the discrepancy.
In your original post you mention the date June 27, 2022, but the timestamp "2022:179:18:42:44.610" is June 28, 2022 at 18:42:44. Is it possible you used a different timestamp in your Python calculation?
Convert the timestamps to datetime
As others have mentioned, these timestamps can be converted to datetime values using the provided formats.
date_obs = "22:179:18:42:44.610";
j2000date = "2000:001:12:00:00.000";
formatOut = "uu:DDD:HH:mm:ss.SSS";
j2000format = "uuuu:DDD:HH:mm:ss.SSS";
Date = datetime(date_obs, InputFormat=formatOut) + calyears(2000)
Date2000 = datetime(j2000date, InputFormat=j2000format)
Calculate the number of Julian days between the timestamps
You can simply convert your datetime values to Julian Dates with the convertTo function (or equivalently, the juliandate function) and perform the calculation with these values.
format longG
jd = convertTo(Date, "juliandate")
jd2000 = convertTo(Date2000, "juliandate")
jd - jd2000
0 Commenti
Vedere anche
Categorie
Scopri di più su Dates and Time in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!