# Convert from Coordinated UTC to datetime?

49 views (last 30 days)
Shayma Al Ali on 5 Nov 2021
Commented: Dave B on 5 Nov 2021
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?

Dave B on 5 Nov 2021
Edited: Dave B on 5 Nov 2021
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 on 5 Nov 2021
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 CommentShowHide None
Dave B on 5 Nov 2021
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()
{"ft_lang_label":"__label__en","ft_lang_prob":0.75231826,"math_prob":0.98573667,"size":1659,"snap":"2022-40-2023-06","text_gpt3_token_len":449,"char_repetition_ratio":0.1123867,"word_repetition_ratio":0.021428572,"special_character_ratio":0.29355034,"punctuation_ratio":0.09467456,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.98324907,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-10-03T11:03:35Z\",\"WARC-Record-ID\":\"<urn:uuid:dd9b52a8-e0ad-4e3e-bf55-395f4363ed31>\",\"Content-Length\":\"137100\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:3de1d3de-8a82-4f8f-95cd-b2038c05323f>\",\"WARC-Concurrent-To\":\"<urn:uuid:f222b2e8-b373-4c74-88c4-dc8ff069dcad>\",\"WARC-IP-Address\":\"104.69.217.80\",\"WARC-Target-URI\":\"https://it.mathworks.com/matlabcentral/answers/1579404-convert-from-coordinated-utc-to-datetime?s_tid=prof_contriblnk\",\"WARC-Payload-Digest\":\"sha1:NXWQ6DAQQTQNLZSP632VBLTDXBGK7AZ3\",\"WARC-Block-Digest\":\"sha1:GT6CFNXOZ3EZ6LBCPSRCQF4XFRP4JWKM\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-40/CC-MAIN-2022-40_segments_1664030337415.12_warc_CC-MAIN-20221003101805-20221003131805-00360.warc.gz\"}"}