5

Using something similar to the date command, is it possible to set the date and time relative to 'now'?

So instead of something like date +%T -s "10:13:13" something like date -s +1h or something similar

5

To set the date, you must log in as root (or su/sudo...)

echo "Time 1 hour ago ... $(date -d "$(date +%F\ %T) 1 hour ago")"
echo "Time now .......... $(date  +%F\ %T)"
echo "Time in 1 hour .... $(date -d "$(date +%F\ %T) 1 hour")"

sudo date +%F\ %T -s "$(date -d "$(date +%F\ %T) 1 hour")"
echo "Adjusted time ..... $(date  +%F\ %T)"

sudo date +%F\ %T -s "$(date -d "$(date +%F\ %T) 1 hour ago")"
echo "Re-adjusted time .. $(date  +%F\ %T)"

output:

Time 1 hour ago ... 2012-04-16 21:31:53
Time now .......... 2012-04-16 22:31:53
Time in 1 hour .... 2012-04-16 23:31:53
[sudo] password for .... 
Adjusted time ..... 2012-04-16 23:31:53
[sudo] password for  .... 
Re-adjusted time .. 2012-04-16 22:31:53
| improve this answer | |
1

Recent date can now set the date relatively with natural language!

Here is an example using GNU coreutils 8.23

# date
Fri Oct  5 16:24:25 UTC 2018
# date --set "10 minutes ago"
Fri Oct  5 16:14:25 UTC 2018
# date --set "10 minutes"
Fri Oct  5 16:24:25 UTC 2018
| improve this answer | |
0

You can change your time zone by doing # dpkg-reconfigure tzdata. I'm not sure how you would change the time by an arbitrary value, though.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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