Display or set system time and date
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
format: output time format.
The available escape sequences for format are as follows:
%% percent sign
%a local abbreviated weekday name (for example, Sun)
%A The full local weekday name (for example, Sunday)
%b local abbreviated month name (for example, Jan)
%B The local full month name (for example, January)
%c local date and time (for example, Thu Mar 3 23:05:25 2005)
%C century, similar to %Y, but omitting the last two digits (for example, 20)
%d day of the month (for example, 01)
%D date, equivalent to %m/%d/%y
%e is a day in January, the format is filled with spaces, equivalent to %_d
%F Complete date; equivalent to %+4Y-%m-%d
%g ISO standard counting week last two digits of the year
%G ISO standard counting week year, usually only useful for %V
%h is equivalent to %b
%H hours, range (00..23)
%I hours, range (00..23)
%j day of the year, range (001..366)
%k hours, padded with spaces, range (0..23), equivalent to %_H
%l hour, padded with spaces, range (1..12), equivalent to %_I
%m month, range (01..12)
%M minutes, range (00..59)
%n newline character
%N nanoseconds, range (000000000..000000000)
%p is used to represent the local AM or PM, blank if unknown
%P is like %p, but in lowercase
%q quarter, range (1..4)
%r local clock time in 12 hours (for example, 11:11:04 PM)
%R 24 hours per minute; equivalent to %H:%M
%s The number of seconds since 00:00 UTC January 1, 1970
%S seconds, range (00..60)
%t horizontal tab character
%T time; equivalent to %H:%M:%S
%u day of the week (1..7), 1 represents Monday
%U The week of the year, starting on Sunday (00..53)
%V ISO standard counting week, this method uses Monday as the start of the week (01..53)
%w day of the week (0..6), 0 represents Sunday
%W The week of the year, starting on Monday (00..53)
%x local date representation (for example, 12/31/99)
%X local time representation (for example, 23:13:48)
%y The last two digits of the year, range (00..99)
%Y year
Numerical time zone format in %z +hhmm format (for example, -0400)
Numerical time zone format in %:z +hh:mm format (for example, -04:00)
Numerical time zone format in %::z +hh:mm:ss format (for example, -04:00:00)
%:::z numeric time zone format, adding ':' compared to the previous format to display the necessary precision (for example, -04, +05:30)
%Z time zone abbreviation (such as EDT)
By default, dates pad numeric fields with zeros; the following optional symbols can follow '%':
- (hyphen) Do not populate the corresponding field.
_ (underscore) fills the corresponding field with spaces.
0 (Number 0) Fills the corresponding field with the number 0.
+ is filled with the number 0, and if the future year is larger than 4 digits, add a '+' sign in front.
^Use uppercase where allowed.
# Convert the default uppercase to lowercase and the default lowercase to uppercase if allowed.
After any flags there is an optional field width, such as decimal; then an optional modifier, E to use the locale's alternative representation where available,
Use O to use the locale's alternative number symbol.
Long options are equivalent to short options
-d, --date=STRING Parse the string and output it in the specified format. The string cannot be 'now'.
--debug annotate parsed dates and send questionable usage to standard error.
-f, --file=DATEFILE Similar to --date; Process one line at a time from DATEFILE.
-I[FMT], --iso-8601[=FMT] Output according to ISO 8601 format, FMT can be 'date' (default), 'hours', 'minutes', 'seconds', 'ns'. For example: 2006-08-14T02:34:56-06:00
-R, --rfc-email Output according to RFC 5322 format, for example: Mon, 14 Aug 2006 02:34:56 -0600
--rfc-3339=FMT output according to RFC 3339 format, FMT can be one of 'date', 'seconds', 'ns', for example: 2006-08-14 02:34:56-06:00
-r, --reference=FILE Display the last modification time of the file.
-s, --set=STRING Set the system time based on a string.
-u, --utc, --universal Display or set Universal Coordinated Time (UTC).
--help Display help information and exit.
--version Display version information and exit.
The return status is success unless illegal options or illegal parameters are given.
# Format output:
date +"%Y-%m-%d"
2009-12-07
# Output yesterday’s date:
date -d "1 day ago" +"%Y-%m-%d"
2012-11-19
#Output after 2 seconds:
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"
# or
date -d@1234567890 +"%F %T"
# Output results
2009-02-13 23:02:30
# Time format conversion:
date -d "2009-12-12" +"%Y/%m/%d %H:%M.%S"
# Output results
2009/12/12 00:00.00
#apache format conversion:
date -d "Dec 5, 2009 12:00:37 AM" +"%Y-%m-%d %H:%M.%S"
# Output results
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"
# Output results
2007-12-05 00:00.37
# Time addition and subtraction operations:
date +%Y%m%d # Display year, month and day
date -d "+1 day" +%Y%m%d # Display the date of the previous day
date -d "-1 day" +%Y%m%d # Display the date of the next day
date -d "-1 month" +%Y%m%d # Display the date of the previous month
date -d "+1 month" +%Y%m%d # Display the date of the next month
date -d "-1 year" +%Y%m%d # Display the date of the previous year
date -d "+1 year" +%Y%m%d # Display the date of the next year
# set time:
date -s # Set the current time. Only root privileges can set it. Others can only view it.
date -s 20120523 # Set to 20120523, which will set the specific time to 00:00:00
date -s 01:01:01 # Set the specific time and the date will not be changed.
date -s "01:01:01 2012-05-23" # This can set the entire time
date -s "01:01:01 20120523" # This can set the entire time
date -s "2012-05-23 01:01:01" # This can set the entire time
date -s "20120523 01:01:01" # This can set the entire time
# Sometimes you need to check how long a set of commands takes:
start=$(date +%s)
nmap wangchujiang.com &> /dev/null
end=$(date +%s)
difference=$(( end - start ))
# Display execution time
echo $difference seconds.
# When you consider outputting a string with time, for example (Current time: 2019/05/19):
# Commonly used methods:
echo "Current time: $(date +"%Y/%m/%d")"
# Another way:
suffix='Current time:'
# Note that variables cannot be replaced if they are replaced with single quotes.
date +"${suffix} %Y/%m/%d"