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?)
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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.
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 '1 hour ago' date
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.