17

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)?

robguinness
  • 16,266
  • 14
  • 55
  • 65

2 Answers2

34

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 ) )
Rody Oldenhuis
  • 37,726
  • 7
  • 50
  • 96
  • 3
    This does not however handle leap seconds. [Click here](http://en.wikipedia.org/wiki/Unix_time) for more info. – Rody Oldenhuis Aug 31 '12 at 08:59
  • 2
    I 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? – robguinness Aug 31 '12 at 09: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. – Rody Oldenhuis Aug 31 '12 at 09:07
  • 1
    Ok, 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. – robguinness Aug 31 '12 at 09: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 :) – Rody Oldenhuis Aug 31 '12 at 09:19
  • 1
    A small matlab snippet to demonstrate Matlab's lack of leap seconds: `(datenum('1999-01-01 00:00:00')-datenum('1998-12-31 23:59:59'))*86400` should return `2`, but returns `1`. – quazgar May 24 '13 at 15:02
31

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 )
Clark
  • 890
  • 8
  • 20
  • 1
    I think this is the best answer now in the newest versions of Matlab. – Léo Léopold Hertz 준영 Mar 06 '16 at 16:15
  • But not in 2013b – wmli Aug 29 '19 at 14:22
  • 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 cmd `dt=datetime(unix_time, 'ConvertFrom','posixtime')` `datestr(dt)`, I am getting the value 31-Jul-47572 04:41:47. What am I doing wrong? – sreeraj t Oct 14 '20 at 09:50