Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Date command

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/03 Report--

The date command displays or sets the system time and date.

Many shell scripts need to print different formats of time or date, and to perform actions based on time and date. Latency is usually used to provide a period of waiting time during script execution. Dates can be printed in multiple formats, or you can use the command to set a fixed format. In UNIX-like systems, the date is stored as an integer of the number of seconds that have elapsed since 00:00:00 Universal Standard time (UTC) on January 1, 1970.

Grammar

Date (option) (parameter) option

-d: displays the date and time indicated by the string. The string must be enclosed in double quotation marks;-s: sets the date and time according to the string. The string must be enclosed in double quotation marks;-u: display GMT;--help: online help;-- version: display version information. Parameters.

Specifies the date-time format to use when displaying

List of date format strings

% H hours, 24 hours (000023)% I hours, 12hrs (010012)% k hours, 24hrs (00023)% l hours, 12hrs (1hrs)% M minutes (000059)% p shows AM or PM%r display time, 12hrs system (hh:mm:ss% p)% s seconds experienced from January 1st, 1970 to 00:00:00, (indicates seconds of unix timestamp)% S display seconds (000059)% T display time 24-hour system (hh:mm:ss) X display time format (H:%M:%S) Z display time zone Date field (CST)% a week abbreviation (Sun~Sat)% A week full name (Sunday~Saturday)% hline% b month abbreviation (Jan~Dec)% B month full name (January~December)% c date and time (Tue Nov 20 14:12:58 2012) d day of a month (01y31)% x D date (mm/dd/yy) j Day of year (001y366) m month (01y12)% w Day of week (0 represents Sunday) W week ordinal of year (000053) Monday is the first day) the last two numbers of the y year (1999 is 99) instance

Formatted output:

Date + "% Y-%m-%d" 2009-12-07

Output yesterday 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 Y-%m-%d 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

General conversion format:

Date-d "2009-12-12" + "% Y/%m/%d% H:%M.%S" 2009-12-12 00Rank 00.00

Apache format conversion:

Date-d "Dec 5, 2009 12:00:37 AM" + "% Y-%m-%d% H:%M.%S" 2009-12-05 12:00:37 AM 00.37

Time wander 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 AM 00.37

Add and subtract operation:

Date +% Y%m%d / / display the day before yesterday 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 previous 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 / / show the date of the next year

Set the time:

Date-s / / sets the current time. Only root permissions can be set. Others can only view date-s 20120523 / / set to 20120523. This will set the specific time to empty 00:00:00date-s 01:01:01 / / set the specific time. The date will not be changed date-s "01:01:01 2012-05-23" / / this will set all time date-s "01:01:01 20120523" / / this will set all time date-s "2012-05-23 01:01:01" / / this will set all time date-s "20120523 01:01:01" / / this will set all time

Sometimes it is necessary to check the time spent on a set of commands, for example:

#! / bin/bashstart=$ (date +% s) nmap man.linuxde.net & > / dev/nullend=$ (date +% s) difference=$ ((end-start)) echo $difference seconds.

Get the unix timestamp of the current time

Date +% s

Use the date command to get the unix timestamp for a specific time

The above demonstration is to obtain the unix timestamp of October 8, 2014.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report