The date command displays or sets the system time and date

Many shell scripts need to print the time or date in different formats and perform operations based on the time and date. Latency is typically used to provide a waiting period during script execution. Dates can be printed in a variety of formats, or fixed formats can be set using commands. In UNIX-like systems, the date is stored as an integer with the size of the number of seconds elapsed since 00:00 UTC, January 1, 1970.

Command function

  • Date can be used to display or set the date and time of the system.

The command parameter

  • %H hours (00-23).
  • %I hours (expressed in 01-12).
  • %K hours (expressed in 0-23).
  • %l hours (expressed in 0-12).
  • %M minutes (00-59).
  • %P AM or PM.
  • %r time (including hours, minutes, seconds, hours are expressed as 12 hours AM/PM).
  • %s Total number of seconds. The start time is 1970-01-01 00:00:00 UTC.
  • %S seconds (in local usage).
  • %T Time (including hours, minutes, seconds, and hours in the 24-hour system).
  • %X time (expressed in local usage).
  • % Z city.
  • %a abbreviation for week.
  • %A The full name of the week.
  • %b Abbreviation of the English name of the month.
  • % the full English name of month B.
  • %c Date and time. Typing only the date directive will show the same result.
  • %d Date (denoted as 01-31).
  • %D Date (including year, month and day).
  • %j The day of the year.
  • %m Month (expressed as 01-12).
  • %U Number of weeks in the year.
  • %w Indicates the number of days in the week. 0 indicates Sunday, 1 indicates Monday, and so on.
  • %x date (in local usage).
  • %y year (00-99).
  • %Y year (four-digit number).
  • %n Inserts a new line on display.
  • %t at display, insert TAB.
  • MM Month (necessary)
  • DD Date (necessary)
  • Hh hours (necessary)
  • Mm minutes (necessary)
  • Ss seconds (optional)
  • Select parameters:
  • -d< string > Displays the date and time indicated by the string. The string must be preceded by double quotation marks.
  • -s< string > Sets the date and time according to the string. The string must be preceded by double quotation marks.
  • -u Displays GMT.
  • –help Online.
  • –version Displays the version information

date -sSet a time

  • Date -s // Set the current time. Only the root user can set the current time.
  • Date -s 20080523 // Set it to 20080523, which will set the time to 00:00:00
  • Date-s 01:01:01 // Set the specific time. The date will not be changed
  • Date -s “01:01:01 2008-05-23” //
  • Date -s “01:01:01 20080523” //
  • Date -s “2008-05-23 01:01:01” //
  • Date -s “20080523 01:01:01” //

Add and subtract

  • Date +%Y%m%d
  • Date +%Y%m%d –date=”+1 day
  • Date +%Y%m%d –date=”-1 day” // Display the date of the next day
  • Date +%Y%m%d –date=”-1 month” // Display the date of last month
  • Date +%Y%m%d –date=”+1 month
  • Date +%Y%m%d –date=”-1 year
  • Date +%Y%m%d –date=”+1 year

Display current time

> date
Mon Mar  1 22:09:51 CST 2021

> date '+%c'
Mon 01 Mar 2021 10:10:13 PM CST

> date '+%D'

> date '+%x'

> date '+%T'

Display date and set time

// Date --date 08:42:00 Mon 01 Mar 2021 08:42:00 PM CST

Display the time after the jump line, and then display the current date

> date '+%T%n%D'

Displays the current timestamp

> date +%s

The timestamp is converted to time

Date -d @ time stamp

>date -d @1614608735 +"%Y-%m-%d %H:%M:%S"
2021-03-01 22:25:35

The time is converted to a timestamp

> date -d "2020-10-11 11:11:11" +"%s

date -dParameters using

Another useful extension to the date command is the -d option. With this powerful option, you can quickly pinpoint a specific date by providing the date as a quoted parameter. The -d option can also tell you which day is a few days from the current date, a few days or weeks from now, or a few days from now. You can do this by enclosing the relative offset in quotation marks as an argument to the -d option.

  • Date -d “Nov 22″ Date -d” Nov 22
  • Date -d ‘2 weeks’ Indicates the date after 2 weeks
  • Date -d ‘next Monday ‘
  • Date -d next-day +%Y%m%d date -d tomorrow +%Y%m%d
  • Date -d last-day +%Y%m%d date -d yesterday +%Y%m%d
  • Date -d last-month +%Y%m date -d last-month +%Y%m
  • Date -d next-month +%Y%m
  • Using the AGO directive, you can get the date in the past:
  • Date -d ’30 days ago’
  • Use a negative number to get the opposite date:
  • Date -d ‘dec 14-2 weeks’ date -d ‘DEC 14-2 weeks’
  • Date -d ‘-100 days’
  • Date -d ’50 days’

The original link:… Wechat official Account: entry station