60

Is there such command for changing the System clock's date and time?

For example:

The current date and time is January 1, 1970 22:30:59:980 where 59 and 980 refers to the seconds and millisecods respectively

And I want to change it to January 2, 1971 23:31:59:990

Is there a command for this?

1

6 Answers 6

66

To set the time in Ubuntu from the terminal, you can use the following command:

sudo date new_date_time_string

where new_date_time_string has to follow the format MMDDhhmmyyyy.ss which is described below:

  • MM 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
  • ss is two digit seconds. Notice the period . before the ss.

Source: Manage Time in Ubuntu Through Command Line.

So, in your particular case, you can use:

sudo date 010224311971.59

Or, you can use:

sudo date --set="1971-01-02 23:31:59.990"  && date --rfc-3339=ns

which is exactly what you asked.

Source:

5
  • what is the actual format of the string date? Sep 8, 2013 at 7:18
  • does that mean the accuracy can only be limited to seconds? Sep 8, 2013 at 7:22
  • @AbelMelquiadesCallejo See my new edits. Sep 8, 2013 at 7:34
  • 1
    Could someone explain? hh is between 00 and 23, but OP and this answer show it being set to 24.
    – Carl H
    Apr 30, 2015 at 8:10
  • @CarlH Great catch on that! See the new edits Apr 30, 2015 at 15:52
21

The easy way:

ntpdate -s ntp.ubuntu.com

To change the timezone:

dpkg-reconfigure tzdata

Probably you need sudo.

2
6

You can use the date command to change the time and you'll need sudo permission to do it; an example to set the date to 27 June 2014 and the time to 1:17 AM is:

sudo date --set "27 Jun 2014 1:17:00"
6

Since systemd was introducted in Ubuntu, the correct way is:

# timedatectl set-time "RFC 3339-compliant string"

e.g.

# timedatectl set-time "2002-10-02T10:00:00-05:00"
# timedatectl set-time "10:35"
# timedatectl set-time "+2 hours"
2
  • 1
    This also works with e.g. sudo date --set="+2 hours", which is exactly what I needed. Oct 4, 2017 at 12:34
  • Setting time zones: timedatectl set-timezone America/New_York Jul 30, 2020 at 15:13
1

Short versions for setting date or time only:

To set the date (will set the time to 0:0:0):

date +%Y%m%d -s "19710102"

To set the time only:

date +%T -s "23:31:59"

You could add the desired ".990" to the seconds.

Read the man page for more options and formats regarding the date command:

man date

Note: you may also want to check or set the hardware clock. Read about it here.

-3
sudo date --set="Mon Feb 16 08:49:56 IST 2015"

That would be correct statement in UBUNTU 14.04.

2
  • 1
    For everyone in the IST timezone only? Does the other answer not work for you?
    – Xen2050
    Feb 16, 2015 at 9:36
  • The question doesn't require changing the timezone. Nov 11, 2015 at 21:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .