69

How come date is converting to wrong time?

result=$(ls /path/to/file/File.*)
#/path/to/file/File.1361234760790

currentIndexTime=${result##*.}
echo "$currentIndexTime"
#1361234760790

date -d@"$currentIndexTime"
#Tue 24 Oct 45105 10:53:10 PM GMT
bobbyrne01
  • 6,295
  • 19
  • 80
  • 150

3 Answers3

130

This particular timestamp is in milliseconds since the epoch, not the standard seconds since the epoch. Divide by 1000:

$ date -d @1361234760.790
Mon Feb 18 17:46:00 MST 2013
andrewdotn
  • 32,721
  • 10
  • 101
  • 130
  • 1
    Yes, its due to timestamp in milliseconds. I have also checked here http://epochconvert.com and get correct result **Tuesday, 19 February 2013, 12:46:00 AM GMT** – Laeeq Feb 19 '17 at 13:42
24

For Mac OS X, it's date -r <timestamp_in_seconds_with_no_fractions>

$ date -r 1553024528
Tue Mar 19 12:42:08 PDT 2019

or

$ date -r `expr 1553024527882 / 1000`
Tue Mar 19 12:42:07 PDT 2019

or

$ date -r $((1553024527882/1000))
Tue Mar 19 12:42:07 PDT 2019
Marcus
  • 2,128
  • 20
  • 22
18

You can use bash arithmetic expansion to perform the division:

date -d @$((value/1000))

Note that "value" is a bash variable with the $ being optional; i.e., $value or value can be used.

armatita
  • 12,825
  • 8
  • 48
  • 49
Richard Jessop
  • 897
  • 1
  • 12
  • 19