Linux date Command
The Linux date command is used to display or set the system date and time.
Syntax
date [OPTION]... [+FORMAT]
date [-u] [-d datestr] [-s datestr] [--utc] [--universal] [--date=datestr] [--set=datestr] [--help] [--version] [+FORMAT] [MMDDhhmm[[CC]YY][.ss]]
Optional Arguments
- -d, --date=STRING: Display time described by STRING, not 'now'.
- -f, --file=DATEFILE: Like --date; once per line from DATEFILE.
- -I[FMT], --iso-8601[=FMT]: Output date/time in ISO 8601 format. FMT can be 'date', 'hours', 'minutes', 'seconds', 'ns'. For example: 2006-08-14T02:34:56-0600.
- -R, --rfc-2822: Output date and time in RFC 2822 format. For example: Mon, 14 Aug 2006 02:34:56 -0600.
- --rfc-3339=FMT: Output date/time in RFC 3339 format. FMT can be 'date', 'seconds', 'ns'. For example: 2006-08-14 02:34:56-06:00.
- -r, --reference=FILE: Display the last modification time of FILE.
- -s, --set=STRING: Set system date and time from STRING.
- -u, --utc, --universal: Show or set Coordinated Universal Time (UTC).
- --help: Display help information.
- --version: Output version information.
FORMAT Arguments
For display purposes, users can set the desired format, which is specified by a plus sign followed by several markers. The available markers are as follows:
%% Output the 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 Local date and time (e.g., Thu Mar 3 23:05:25 2005)
%C Century, similar to %Y but omits the last two digits (e.g., 20)
%d Day of the month (01..31)
%D Date, equivalent to %m/%d/%y
%e Day of the month, space-padded ( 1..31); equivalent to %_d
%F Full date; equivalent to %Y-%m-%d
%g Last two digits of the ISO week-based year
%G ISO week-based year, usually only useful with %V
%h Equivalent to %b
%H Hour (00..23)
%I Hour (01..12)
%j Day of the year (001..366)
%k Hour, space-padded ( 0..23); equivalent to %_H
%l Hour, space-padded ( 1..12); equivalent to %_I
%m Month (01..12)
%M Minute (00..59)
%n Newline character
%N Nanoseconds (000000000..999999999)
%p Local AM or PM, blank if not known
%P Like %p, but lowercase
%r Local 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; equivalent to %H:%M
%s Seconds since 1970-01-01 00:00:00 UTC
%S Seconds (00..60)
%t Horizontal tab character
%T Time; equivalent to %H:%M:%S
%u Day of the week (1..7); 1 represents Monday
%U Week number of the year, Sunday as the first day of the week (00..53)
%V ISO week number, Monday as the first day of the week (01..53)
%w Day of the week (0..6), 0 represents Sunday
%W Week number of the year, Monday as the first day of the week (00..53)
%x Local date format (e.g., 12/31/99)
%X Local time format (e.g., 23:13:48)
%y Last two digits of the year (00..99)
%Y Year
%z Numeric time zone in +hhmm format (e.g., -0400)
%:z Numeric time zone in +hh:mm format (e.g., -04:00)
%::z Numeric time zone in +hh:mm:ss format (e.g., -04:00:00)
This is a Chinese to English translation. Please provide the English translation for this text without any explanations or additional text.
Chinese: %:::z Digital time zone format, adding ':' to the previous format to display 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, and the time format is `MMDDhhmm[[CC]YY][.ss]`, where **MM** is the month, **DD** is the day, **hh** is the hour, **mm** is the minute, **CC** is the first two digits of the year, **YY** is the last two digits of the year, and **ss** is the seconds.
Usage 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 meaningless zeros from hours, minutes, and seconds, such as changing 08:09:04 to 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 up-to-date and correct upon the next reboot.
### Examples
Display the current time
date
Tue May 24 09:29:43 CST 2022
date '+%c'
Tue 24 May 2022 09:30:03 AM CST
date '+%D' //Display full time
05/24/22
date '+%x' //Display numeric date
05/24/2022
date '+%T' //Display date, year with four digits
14:09:31
date '+%X' //Display 24-hour format
09: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 the 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 conversion and travel:
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 your own format:
date '+usr_time: $1:%M %P -hey'
usr_time: $1:16 PM -hey
Display time followed by a newline, then display the 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 operations:
date +%Y%m%d # Display year, month, and day date -d "+1 day" +%Y%m%d # Display the next day's date date -d "-1 day" +%Y%m%d # Display the previous day's date date -d "-1 month" +%Y%m%d # Display the previous month's date date -d "+1 month" +%Y%m%d # Display the next month's date date -d "-1 year" +%Y%m%d # Display the previous year's date date -d "+1 year" +%Y%m%d # Display the next year's date
Set time:
date -s # Set the 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 the specific time, does not change the date date -s "01:01:01 2012-05-23" # This can set the entire time
date -s "01:01:01 20120523" # This sets the entire time date -s "2012-05-23 01:01:01" # This sets the entire time date -s "20120523 01:01:01" # This sets the entire time ```