I know about
date -d @<timestamp in seconds>
and
awk '{print strftime("%c", <timestamp in seconds>)}'
but what if I have milliseconds. Is there trivial way to do this without dropping the final three characters of the millisecond-timestamp (not that dropping characters is difficult, but I would think there'd be a one-step way for such a straightforward task)?
Use the Date() constructor to convert milliseconds to a date, e.g. const date = new Date(timestamp) . The Date() constructor takes an integer value that represents the number of milliseconds since January 1, 1970, 00:00:00 UTC and returns a Date object.
You can simply use the fromtimestamp function from the DateTime module to get a date from a UNIX timestamp. This function takes the timestamp as input and returns the corresponding DateTime object to timestamp.
date +"%T. %6N" returns the current time with nanoseconds rounded to the first 6 digits, which is microseconds. date +"%T. %3N" returns the current time with nanoseconds rounded to the first 3 digits, which is milliseconds.
What I have in my bash profile on Mac:
day() { date -r $(($1 / 1000)) }
day 1486743904359
returns Fri Feb 10 08:25:04 PST 2017
Instead of dropping characters, you could divide by 1000:
awk '{print strftime("%c", ( <timestamp in milliseconds> + 500 ) / 1000 )}'
Or:
date -d @$( echo "(MilliSecondTimeStamp + 500) / 1000" | bc)
Or (MacOS):
gdate -d @$( echo "(MilliSecondTimeStamp + 500) / 1000" | bc)
Edit: Adjusted for the quotients instead of division. Edit2: Thx zeekvfu, fixed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With