# Is there a simple way to get the number of milliseconds since the epoch corresponding to a given date?

UnixTime@DateObject@"2017-05-05T01:02:30.5Z" gives the seconds, rounded to an integer, so I can't simply multiply it by 1000 to get the milliseconds. How can I get milliseconds without doing date arithmetic?

• "...gives the seconds, as an integer, so I can't multiply it by 1000." - I'm having trouble understanding this. Why wouldn't you be able to multiply an integer by 1000? – J. M.'s discontentment Nov 3 '17 at 18:20
• Why not use AbsoluteTime instead of UnixTime? – Carl Woll Nov 3 '17 at 18:23
• Sorry, "...gives the seconds, as an integer, so I can't multiply it by 1000." was unclear. I've updated it. I meant that, since the fractional section is gone, I can't multiply it by 1000 to get the millisecond value. – Ashley Nov 3 '17 at 18:27
• I'm using UnixTime instead of AbsoluteTime because I want to compute in GMT. – Ashley Nov 3 '17 at 18:28
• Try Block[{\$TimeZone = 0}, UnitConvert[DateDifference[DateObject["1970-01-01T00:00:00Z"], DateObject["2017-05-05T01:02:30.5Z"], "Seconds"], "Milliseconds"]]. – J. M.'s discontentment Nov 3 '17 at 18:37

There is a fixed offset between AbsoluteTime and UnixTime, so you can use AbsoluteTime instead of UnixTime if you want subsecond resolution. For example:

ut = (#-2208963600&)@*AbsoluteTime;


Then:

UnixTime[Today]
ut[Today]


1509692400

1509692400

and:

UnixTime[Now]
ut[Now]


1509740426

1.509740426703258*10^9

UnixTime @ DateObject @ "2017-05-05T01:02:30.5Z"
ut @ DateObject @ "2017-05-05T01:02:30.5Z" //InputForm


1493971350

1.4939713505*^9

• DateObject @ "2017-05-05T01:02:30.5Z" is not what you think it is. mathematica.stackexchange.com/q/144764/5478 – Kuba Nov 3 '17 at 20:48
• @Kuba I just copied what the OP had, you should probably be commenting on the OP use of that idiom instead. My point was that my version ut provides the fractional seconds they wanted. – Carl Woll Nov 3 '17 at 21:20
• That was fyi, nothing more. And yes I could comment on OP but it already has more comments so here it can get more attention. – Kuba Nov 3 '17 at 21:29
• Yes, I noticed that it doesn't parse the Z at the end. Thanks for pointing it out. – Ashley Nov 6 '17 at 15:30

The best I could come up with is f[d_String] := Module[{do = DateObject[d, TimeZone -> "GMT"]}, 1000 * UnixTime@DateObject@do + DateValue[DateObject@do, "Millisecond"]].

If you want the current time in milliseconds since Epoch,

ToExpression[ToString[UnixTime[]] <> DateString["Millisecond"]]


I don't know how accurate it is given there are two functions called at different times so I can only assume there is a slight variation between the "true" time with milliseconds and this solution.

UnixTime[]

1509763110

ToExpression[ToString[UnixTime[]] <> DateString["Millisecond"]]

1509763110487