MATLAB Answers

How can I convert datetime into double?

349 views (last 30 days)
bluebar on 11 Dec 2019
Answered: Eric Sofen on 13 Dec 2019
I have a data which is 60 x 4320 datetime, each cell is 2019/01/01 00:00:00 and want to convert this into 60 x 4320 double.
I tried to use datevec(data) but it resulted 259200 x 6 double, one row one character, like
2019 01 01 00 00 00
2019 01 01 00 10 00
2019 01 01 00 20 00
  1 Comment
Stephen on 11 Dec 2019
datenum does what you ask for... but probably is not what you need: what do you want to do with double that you don't think is possible with datetime?

Sign in to comment.

Answers (2)

galaxy on 12 Dec 2019
You can use datenum to convert each your data to double.
>> t = datetime('now')
t =
2019/12/12 09:43:30
>> DateNumber = datenum(t)
DateNumber =
After that, if you want to convert back:
>> datetime(DateNumber,'ConvertFrom','datenum')
ans =
2019/12/12 09:43:30
hope it helps

Eric Sofen
Eric Sofen on 13 Dec 2019
What do you want the doubles to represent?
Datenum returns a double represents days since 0 CE.
The posixtime function would give you a double representing seconds since the Unix epoch (00:00:00 1-Jan-1970 UTC).
The juliandate function gives a double representing days since...
The trouble with using double to represent dates and times is that there isn't one definition (there are also precision-related issues over long spans of time).
I'm curious if you'd be willing to share what you want to do with the doubles, and why you can't do it using datetime?


Community Treasure Hunt

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

Start Hunting!

Translated by