77

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 
6
  • 2
    1361234760790 / (60*60*24*365) = 43164.47 years Commented May 1, 2013 at 2:03
  • not sure what that explains? Commented May 1, 2013 at 2:07
  • 2
    Roughly 43164 + 1970 ~= 45105 (43135.8 + 1970 to be more accurate) so the date is right Commented May 1, 2013 at 2:09
  • interesting, how can this date be printed correctly? Commented May 1, 2013 at 2:12
  • using epochconverter.com, i get GMT: Tue, 19 Feb 2013 00:46:00 GMT which is desired result Commented May 1, 2013 at 2:21

3 Answers 3

139

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 
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, its due to timestamp in milliseconds. I have also checked here epochconvert.com and get correct result Tuesday, 19 February 2013, 12:46:00 AM GMT
29

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 

Comments

19

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.