Linux Comm Date
[ Linux Command Manual](#)
The Linux **date** command can be used to display or set the system date and time.
### Syntax
date ... [+FORMAT] date [+FORMAT] [MMDDhhmm[YY][.ss]]
#### Optional Parameters
* **-d, --date=STRING**: Display time described by STRING, not 'now'.
* **-f, --file=DATEFILE**: Like --date; process each line of DATEFILE.
* **-I, --iso-8601**: Output date/time in ISO 8601 format. FMT is 'date' (default), 'hours', 'minutes', 'seconds', or 'ns'. Used to set the precision of date and time, e.g., 2006-08-14T02:34:56-0600.
* **-R, --rfc-2822**: Output date and time in RFC 5322 format, e.g., Mon, 14 Aug 2006 02:34:56 -0600.
* **--rfc-3339=FMT**: Output date and time in RFC 3339 format. FMT is 'date', 'seconds', or 'ns'. Used to set the precision of date and time, e.g., 2006-08-14 02:34:56-06:00.
* **-r, --reference=FILE**: Display the last modification time of FILE.
* **-s, --set=STRING**: Set time described by STRING.
* **-u, --utc, --universal**: Print or set Coordinated Universal Time (UTC).
* **--help**: Display this help and exit.
* **--version**: Output version information.
#### FORMAT Parameter
For display, the user can specify the desired format. The format is a plus sign followed by several markers. The available markers are as follows:
%% Output the literal character %a Abbreviated weekday name (Sun..Sat)%A Full weekday name (Sunday..Saturday). %b Abbreviated month name (e.g., Jan)%B 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, but omit last two digits (e.g., 20)%d Day of month (01..31)%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 2 digits of year of ISO 8601 week-based year%G Year of ISO 8601 week-based year, usually only useful with %V %h Equivalent to %b %H Hour (00..23)%I Hour (01..12)%j Day of year (001..366)%k Hour, space padded ( 0..23); same as %_H %l Hour, space padded ( 1..12); same as %_I %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 unknown%P Like %p, but lowercase%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 Insert a tab%T Time; same as %H:%M:%S %u Day of week (1..7); 1 represents Monday%U Week number of year, with Sunday as first day of week (00..53)%V ISO 8601 week-based week number, with Monday as first day of week (01..53)%w Day of week (0..6); 0 represents 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 time zone (e.g., -0400)%:z +hh:mm numeric time zone (e.g., -04:00)%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)%:::z Numeric time zone with necessary precision (e.g., -04, +05:30)%Z Time zone abbreviation (e.g., EDT)
If not starting with a plus sign, it indicates setting the time, with the format MMDDhhmm[YY][.ss], where **MM** is month, **DD** is day, **hh** is hour, **mm** is minute, **CC** is the first two digits of the year, **YY** is the last two digits of the year, and **ss** is seconds.
Permissions: All users.
When you do not want meaningless zeros (e.g., 1999/03/07), you can insert a - symbol in the marker, for example, date '+%-H:%-M:%-S' will remove the meaningless zeros from hours, minutes, and seconds, so the original 08:09:04 becomes 8:9:4. Additionally, only those with permission (e.g., root) can set the system time.
After changing the system time as root, remember to use clock -w to write the system time to **CMOS**, so that the system time will remain the latest correct value on the next boot.
### Examples
Display current time
# dateTue May 24 09:29:43 CST 2022# date '+%c' Tue 24 May 2022 09:30:03 AM CST # date '+%D' //Display full time05/24/22# date '+%x' //Display numeric date05/24/2022# date '+%T' //Display date with four-digit year14:09:31# date '+%X' //Display 24-hour format09:31:31 AM
Formatted output:
# date +"%Y-%m-%d"2009-12-07
Output yesterday's date:
# date -d "1 day ago" +"%Y-%m-%d"2012-11-19
Output time 2 seconds later:
# date -d "2 second" +"%Y-%m-%d %H:%M.%S"2012-11-20 14:21.31
The legendary 1234567890 seconds:
# date -d "1970-01-01 1234567890 seconds" +"%Y-%m-%d %H:%M:%S"2009-02-13 23:02:30
Or:
# date -d@1234567890 +"%F %T"2009-02-13 23:02:30
Time format conversion:
# date -d "2009-12-12" +"%Y/%m/%d %H:%M.%S"2009/12/12 00:00.00
Apache format conversion:
# date -d "Dec 5, 2009 12:00:37 AM" +"%Y-%m-%d %H:%M.%S"2009-12-05 00:00.37
Time travel after format conversion:
# date -d "Dec 5, 2009 12:00:37 AM 2 year ago" +"%Y-%m-%d %H:%M.%S"2007-12-05 00:00.37
Output in custom format
# date '+usr_time: $1:%M %P -hey' usr_time: $1:16 PM -hey
Display time, then newline, then current date
date '+%T%n%D'
Display month and day
date '+%B %d'
Display date and set time (12:34:56)
date --date '12:34:56'
Time addition and subtraction:
date +%Y%m%d # Display year, month, day date -d "+1 day" +%Y%m%d # Display next day's date date -d "-1 day" +%Y%m%d # Display previous day's date date -d "-1 month" +%Y%m%d # Display previous month's date date -d "+1 month" +%Y%m%d # Display next month's date date -d "-1 year" +%Y%m%d # Display previous year's date date -d "+1 year" +%Y%m%d # Display next year's date
Set time:
date -s # Set current time, only root can set, others can only view date -s 20120523 # Set to 20120523, this will set the specific time to 00:00:00 date -s 01:01:01 # Set specific time, without changing the date date -s "01:01:01 2012-05-23" # This can set all time date -s "01:01:01 20120523" # This can set all time date -s "2012-05-23 01:01:01" # This can set all time date -s "20120523 01:01:01" # This can set all time
[ Linux Command Manual](#)
YouTip