How to set date/time from unix timestamp under bash

luzik picture luzik · Feb 22, 2013 · Viewed 11k times · Source

How to set date/time from unix timestamp under bash

root@M501 />date
Thu Jan  1 00:10:49 UTC 1970
root@M501 />date +%s
652
root@M501 />date +%s -s "`date +%s`"
date: invalid date `662'

as You can see date +%s -s "2323123" do not work :/

[SOLVED] ..under bash i can use

date +%s -s "@`date +%s`"

or

date -s @1361529589

Thanks!

Question #2 How to achieve this under busybox?

root@M501 />date -s @1361529589
date: invalid date `@1361529589'

maybe there is way like

echo '1361529589' > /dev/unix_time_stamp_or_whatever ? :)

Answer

dogbane picture dogbane · Feb 22, 2013

You need to prefix the number with the @ symbol so that the date command knows that it represents the number of seconds since the Epoch. Try this:

date +%s -s @`date +%s`