I have some data files with Unix timestamps (in this case, number of milliseconds since Jan 1, 1970 00:00 UTC). I would like to convert these to human-friendly date/time strings (e.g. 31-Aug-2012 11:36:24) in Matlab. Is there an easy way to do this in Matlab, or am I better off using an external library (e.g. java.text.SimpleDateFormat)?
2 Answers
How about
date = datestr(unix_time/86400 + datenum(1970,1,1))
if unix_time
is given in seconds, unix_time/86400
will give the number of days since Jan. 1st 1970. Add to that the offset used by Matlab's datenum
(datenum(0000,1,1) == 1
), and you have the amount of days since Jan. 1st, 0000. This can be easily converted to human-readable form by Matlab's datestr
.
If you have milliseconds, just use
date = datestr(unix_time/86400/1000 + datenum(1970,1,1))
Wrapped in functions, these would be
function dn = unixtime_to_datenum( unix_time )
dn = unix_time/86400 + 719529; %# == datenum(1970,1,1)
end
function dn = unixtime_in_ms_to_datenum( unix_time_ms )
dn = unix_time_ms/86400000 + 719529; %# == datenum(1970,1,1)
end
datestr( unixtime_to_datenum( unix_time ) )
-
3This does not however handle leap seconds. Click here for more info. Aug 31, 2012 at 8:59
-
2I knew that Unix timestamps do not include leap seconds. Are you implying that Matlab's date/time functions do include leap seconds? So I have to figure out a way to account for this difference? Aug 31, 2012 at 9:04
-
1@robguinness AFAIK, neither unix time or matlab's date/time functions handles leap seconds correctly. I put it in a comment, because for many people, the answer will be sufficient. But if you need leapseconds, you'll have to devise something around that. Aug 31, 2012 at 9:07
-
1Ok, thanks. I'd rather leave leap seconds out of the equation. As long as I can convert from unix timestamp to a friendly date/time, I don't mind if it's off from the wall clock time by a few seconds. I just wanted to check that the conversion would be the same in Matlab as in other systems where the unix epoch is commonly used. Aug 31, 2012 at 9:09
-
7@robguinness There really should be an IEEE standard time system, and libraries for it in every programming language...every new system just seems to use its own ad-hoc standard. Unix time is stupid in many respects as well; it only has the advantage of being ubiquitous :) Aug 31, 2012 at 9:19
Newer versions of MATLAB (verified in R2015a) have a datetime type that is useful for working with and formatting dates and times. You can convert UNIX timestamps into a MATLAB datetime with
dt = datetime( unix_time, 'ConvertFrom', 'posixtime' );
and then use datestr as before for formatting as a string
datestr( dt )
-
1I think this is the best answer now in the newest versions of Matlab. Mar 6, 2016 at 16:15
-
-
I am using Matlab2016a. I have variable unix_time=1.439078416907000e+12, which is milliseconds since 1970-01-01 00:00:00.0 UTC. When I run the cmd
datestr(unix_time/86400/1000 + datenum(1970,1,1))
, it is giving correct answer which is 09-Aug-2015 00:00:16. But if I run the cmddt=datetime(unix_time, 'ConvertFrom','posixtime')
datestr(dt)
, I am getting the value 31-Jul-47572 04:41:47. What am I doing wrong? Oct 14, 2020 at 9:50