10

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
Alexiel
  • 149

1 Answers1

14

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
phemmer
  • 71,831
  • How can I interpret the number after the dot, here 012? Is it a decimal number? How can I convert the current date to ms? With DATE=$(date)? – Timo Feb 16 '22 at 19:57
  • just a note that %N is not available on the BSDs date – Matt Nov 26 '23 at 00:36