2

I need to temporarily change the date to debug a script. I ran into a unexpected problem:

root@xxx:/# LC_ALL=C date
Wed Jan 31 17:09:02 EET 2018
root@xxx:/# LC_ALL=C date -s "2018-01-10 17:09:30"
Wed Jan 10 17:09:30 EET 2018
root@xxx:/# LC_ALL=C date
Wed Jan 31 17:09:28 EET 2018

note: I have a ru_RU locale, so I use LC_ALL=C for your convenience. Using the commands without LC_ALL changes nothing.

I guessed that I did something wrong with syntax or read man incorrectly. So I used instructions from this thread and it solved nothing. Attempts to set hwclock to match date resulted in nothing:

root@xxx:/# LC_ALL=C hwclock --systohc
root@xxx:/# LC_ALL=C hwclock --show
Wed Jan 31 17:18:17 2018  .451473 seconds
root@xxx:/# man hwclock
root@xxx:/# hwclock --set --date='2011-08-14 16:45:05'
root@xxx:/# LC_ALL=C hwclock --show
Sun Aug 14 16:45:09 2011  .389008 seconds
root@xxx:/# LC_ALL=C date
Wed Jan 31 17:21:07 EET 2018

How can I change date?

System:

root@xxx:/# cat /etc/lsb-release 
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=16.04
DISTRIB_CODENAME=xenial
DISTRIB_DESCRIPTION="Ubuntu 16.04.3 LTS"

UPDATE for @Yaron

kovjr@xxx:~$ LC_ALL=C sudo date 120622432007.55
[sudo] password for kovjr: 
Thu Dec  6 22:43:55 EET 2007
kovjr@xxx:~$ LC_ALL=C date
Wed Jan 31 18:19:23 EET 2018
userene
  • 276

1 Answers1

0

As described here:

View Time

To view the current date and time, the following command will be enough

date

Set Time

To change time means to set a new time. To set time in Ubuntu (or any Linux), just run the following command

sudo date newdatetimestring

where newdatetimestring has to follow the format nnddhhmmyyyy.ss which is described below

nn is a two digit month, between 01 to 12
dd is a two digit day, between 01 and 31, with the regular rules for days according to month and year applying
hh is two digit hour, using the 24-hour period so it is between 00 and 23
mm is two digit minute, between 00 and 59
yyyy is the year; it can be two digit or four digit: your choice. I prefer to use four digit years whenever I can for better clarity and less confusion
ss is two digit seconds. Notice the period ‘.’ before the ss.

Let’s say you want to set your computer’s new time to December 6, 2007, 22:43:55, then you would use:

sudo date 120622432007.55

Example:

$ sudo date 120622432007.55
[sudo] password for xyz: 
Thu Dec  6 22:43:55 IST 2007
$ date
Thu Dec  6 22:43:56 IST 2007
$
Yaron
  • 13,453