To retrieve and set the system time of Linux systems is very easy. Virtually every Linux system comes with the required tools for that task.
This article illustrates how to fetch / modify the system time at command line.
There are various ways to set the local system time. The following recipe will work without restarting any machines and it will work seamlessly in custom timezones.
The command date provides a default behaviour when called without any command line parameters. Additionally it is possible to specify a format string to tell ‘date‘ what is intended to do. The format string starts with a ‘+’, contains custom strings and a number of directives you may find at the end of this article.
2. Get the System Time
Linux distributions come with a command line tool called date. By default it prints the system time considering your local timezone. By providing a custom format string the output may be treaked to fit your needs.
# print the system time $ date Wed Jun 23 15:43:23 CEST 2010 # print a custom date (same as %Y-%m-%d) $ date +%F 2010-06-23 # print a custom time format $ date +%H:%M:%S.%N 15:46:47.230995518 # ...and a custom string $ date "+Today is %A in %B, it's the week no. %V" Today is Wednesday in June, it's the week no. 25
3. Set the System Time
As you may expect we will utilize the same command to set the system time. You can modify every single part of the time and date string. To set the system time utilize the command line option ‘-s‘. Let’s take a look at some examples.
# set the date (and time) $ date -s 100623 Wed Jun 23 00:00:00 CEST 2010 # set the date (and time) $ date +F -s 2010-06-23 2010-06-23 # ask the system for the new system time $ date Wed Jun 23 00:00:00 CEST 2010 # set the time $ date +%T -s "16:07:00" 16:07:00 # ask the system for the new system time $ date Wed Jun 23 16:07:00 CEST 2010 # set time and date $ date +"%F %T" -s "2010-06-23 16:10:15" 2010-06-23 16:10:15 # ask the system for the new system time $ date Wed Jun 23 16:10:15 CEST 2010
4. The Format String
The format string may consist of a number of directives that are defined as follows.
FORMAT controls the output. Interpreted sequences are: %% a literal % %a locale's abbreviated weekday name (e.g., Sun) %A locale's full weekday name (e.g., Sunday) %b locale's abbreviated month name (e.g., Jan) %B locale's full month name (e.g., January) %c locale's date and time (e.g., Thu Mar 3 23:05:25 2005) %C century; like %Y, except omit last two digits (e.g., 20) %d day of month (e.g, 01) %D date; same as %m/%d/%y %e day of month, space padded; same as %_d %F full date; same as %Y-%m-%d %g last two digits of year of ISO week number (see %G) %G year of ISO week number (see %V); normally useful only with %V %h same as %b %H hour (00..23) %I hour (01..12) %j day of year (001..366) %k hour ( 0..23) %l hour ( 1..12) %m month (01..12) %M minute (00..59) %n a newline %N nanoseconds (000000000..999999999) %p locale's equivalent of either AM or PM; blank if not known %P like %p, but lower case %r locale's 12-hour clock time (e.g., 11:11:04 PM) %R 24-hour hour and minute; same as %H:%M %s seconds since 1970-01-01 00:00:00 UTC %S second (00..60) %t a tab %T time; same as %H:%M:%S %u day of week (1..7); 1 is Monday %U week number of year, with Sunday as first day of week (00..53) %V ISO week number, with Monday as first day of week (01..53) %w day of week (0..6); 0 is Sunday %W week number of year, with Monday as first day of week (00..53) %x locale's date representation (e.g., 12/31/99) %X locale's time representation (e.g., 23:13:48) %y last two digits of year (00..99) %Y year %z +hhmm numeric timezone (e.g., -0400) %:z +hh:mm numeric timezone (e.g., -04:00) %::z +hh:mm:ss numeric time zone (e.g., -04:00:00) %:::z numeric time zone with : to necessary precision (e.g., -04, +05:30) %Z alphabetic time zone abbreviation (e.g., EDT) By default, date pads numeric fields with zeroes. The following optional flags may follow `%': - (hyphen) do not pad the field _ (underscore) pad with spaces 0 (zero) pad with zeros ^ use upper case if possible # use opposite case if possible
- Configure Your Local Timezone
- Set Date and Time Automatically (NTP)