How to manipulate date keeping the same time via command line
This is an example of what ( I think ) you want. It will change the year only to last year for 4 minutes then back to current date again. Your machine needs to be connected to the Internet for this to work:
sudo bash -c "timedatectl set-ntp off && date -s 'last year' && sleep 4m && timedatectl set-ntp on"
For the below to work, you should first go to Settings → Date & Time and disable Automatic Date & Time.
You can then use the following command as a (long) one-liner:
sudo date --set="2010-05-02 $(date '+%T')" && sleep 10 && sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"
The above command:
-
Gets the current system time:
date '+%T'
and sets the date to the one you wish, keeping the current time:
sudo date --set="2010-05-02 $(date '+%T')"
In this example the date, which you may change to what you need, is
2010-05-02
. -
Keeps the altered date for the specified time interval using the
sleep
command. Here I have usedsleep 10
to keep the altered time for 10 seconds. You can add anm
,h
, ord
suffix tosleep
's argument (here10
) to specify the time in minutes, hours, or days, respectively. -
Reverts the time to the current time (requires internet access):
sudo date -s "$(wget -qSO- --max-redirect=0 google.com 2>&1 | grep Date: | cut -d' ' -f5-8)Z"
Credits to Shrukul Habib.