Bash – How to convert a date to milliseconds since Unix epoch in bash

bashtimestamps

I've got a date in format:

22-Sep-2014 10:32:35

I need a 13-digit timestamp, but when I convert this way

time=$(date -d "$DATE" '+%s')

I get a 10-digit number

When I try

tt=$(date -d "$DATE");
time=$($tt +'%s * 1000 + %-N / 1000000')

I get

line 22: Mon: command not found

Best Answer

Your second attempt was close, just need to tell the date command to add milliseconds to the end. You can do this with the %N formatter. %N expands to nanoseconds, and then you can just truncate that by using %3N.
Note however that your example input time does not have any milliseconds on it, so you could just add .000 to the end.
Assuming this is not what you want, here's an example that provides millisecond accuracy:

$ DATE="22-Sep-2014 10:32:35.012"
$ date -d "$DATE" +'%s.%3N'
1411396355.012
Related Question