フィルターのクリア

Convert from Coordinated UTC to datetime?

51 ビュー (過去 30 日間)
Shayma Al Ali
Shayma Al Ali 2021 年 11 月 5 日
コメント済み: Dave B 2021 年 11 月 5 日
I have a list of times formatted in coordinated UTC time. For example, the first data entry point I have is
1.511092230713565e+05
where the first six digits are the year, month and day (so 11/09/2015) and the rest of the digits are the fraction of the day. How do I extract the the first six digits and then convert the rest of the digits so I can convert the time to a datenum array?

回答 (2 件)

Dave B
Dave B 2021 年 11 月 5 日
編集済み: Dave B 2021 年 11 月 5 日
You can do this easily with datetime:
x=1.511092230713565e+05;
% The date part is the part before the decimal, so use floor
d = datetime(string(floor(x)),'InputFormat','yyMMdd')
d = datetime
09-Nov-2015
% The time part is fractions of a day, remove the date part and call days
% to get a duration
t = days(x-floor(x))
t = duration
0.22307 days
% The result is the date part (which will be midnight) plus the fraction of
% a day
dt = d+t
dt = datetime
09-Nov-2015 05:21:13
% If you really must use datenum (though this is not recommended, datetime is so much better!)
datenum(dt)
ans = 7.3628e+05

Star Strider
Star Strider 2021 年 11 月 5 日
A different approach —
format long g
nr = 1.511092230713565e+05;
dt = datetime(num2str(fix(nr)),'InputFormat','yyMMdd') + timeofday(datetime(datevec(rem(nr,1))))
dt = datetime
09-Nov-2015 05:21:13
dn = datenum(dt)
dn =
736277.223071357
Also, Specify Time Zones might be of interest.
.
  1 件のコメント
Dave B
Dave B 2021 年 11 月 5 日
I like the rem(nr,1) better than my x-floor(x), and I think fix is technically the accurate choice if there might be some BC dates?
Less sure about timeofday(datetime(datevec())) vs. days()

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by