I know commands for setting specific time and/or date, but can't find ones which move the time relative to the current time.

What are the commands to move the time forward/backward x seconds/minutes/hours? (And possibly also days/months/years?)

up vote 10 down vote accepted

The command to set the system time is date. You need to be root to set the system time. date sets the time to the given time, not to a relative amount from the current time, because that latter behavior would be pretty pointless. You can build a command that modifies the current time by a relative amount by making a calculation on the output of date and feeding it back to date, e.g. (on non-embedded Linux)

date $(date +%m%d%H%M%Y.%S -d '1 hour ago')

Beware that if you're running a timekeeping system such as NTP, changing the clock like this will confuse it. Stop it first.

Running date sets the system time, not the hardware clock. Under Linux, run hwclock --systohc to copy the system time to the hardware clock; this is done automatically on a clean shutdown.

If you wanted to see the time in a different timezone, forget all this and set the desired timezone instead. Under Linux, run tzselect to set the system timezone. To run a program in a different timezone, set the TZ environment variable, e.g.

export TZ=Asia/Tokyo
emacs

If you want to run a program and make it believe that the time is different from what it really is, run it under the program faketime.

faketime '1 hour ago' date
  • 2
    Or date -Ins -s $(date -Ins -d '-1 hour') – Zaz Oct 29 '15 at 19:20
  • @Zaz you could post your comment as a new answer – Saullo G. P. Castro Dec 17 '17 at 0:57
  • 1
    @SaulloCastro: Done. – Zaz Dec 17 '17 at 17:27
date -Ins -s $(date -Ins -d '-1 hour')
  • 1
    When going from a comment to an answer, it is preferred to add some prose explaining how this works, and maybe why this answer is better/different from the others. – Stephen Rauch Dec 17 '17 at 17:55

You can also use the following format to get the amount of time ahead of the current time:

$ date --date='1 month'
Sun Sep 17 15:01:11 CST 2017
$ date --date='1 month'
Sun Sep 17 15:01:15 CST 2017
$ date --date='1 month 2 hours'
Sun Sep 17 17:01:23 CST 2017
$ date --date='1 month 2 hours 3 minutes'
Sun Sep 17 17:04:30 CST 2017

Also you can use ago for a time before the current date:

$ date
Fri Aug 18 16:14:41 CST 2017
$ date --date='1 month 2 days ago'
Sat Sep 16 16:14:43 CST 2017

You can use the --set option for setting time but adding a + sign before the number of hours, minutes, etc, that you want to add to the actual date:

$ date
Thu Aug 17 15:07:00 CST 2017
$ date --set='+1 day +1 hour'
Fri Aug 18 16:07:24 CST 2017
$ date
Fri Aug 18 16:07:31 CST 2017
$ date --set='+1 month 3 days +1 hour'
Thu Sep 21 17:07:42 CST 2017

I hope this is helpful.

If I remember right, in UNIX SVR4, there was a "-a" option to the "date" command, which would allow the superuser to adjust the clock forward or backward some number of seconds.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.