date命令显示当前日期和时间。 它还可以指定的格式显示或计算特定日期。 超级用户(root)可以使用它来设置系统时钟。
使用格式: date [OPTION]... [+FORMAT]
date --help显示详细选项(man date查看完整说明):
$ date --help
Usage: date [OPTION]... [+FORMAT]
or: date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
Display the current time in the given FORMAT, or set the system date.
-d, --date=STRING display time described by STRING, not `now'
-f, --file=DATEFILE like --date once for each line of DATEFILE
-r, --reference=FILE display the last modification time of FILE
-R, --rfc-2822 output date and time in RFC 2822 format.
Example: Mon, 07 Aug 2006 12:34:56 -0600
--rfc-3339=TIMESPEC output date and time in RFC 3339 format.
TIMESPEC=`date', `seconds', or `ns' for
date and time to the indicated precision.
Date and time components are separated by
a single space: 2006-08-07 12:34:56-06:00
-s, --set=STRING set time described by STRING
-u, --utc, --universal print or set Coordinated Universal Time
--help display this help and exit
--version output version information and exit
FORMAT controls the output. Interpreted sequences are:
%% a literal %
%a locale's abbreviated weekday name (e.g., Sun)
%A locale's full weekday name (e.g., Sunday)
%b locale's abbreviated month name (e.g., Jan)
%B locale's 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, except omit last two digits (e.g., 20)
%d day of month (e.g, 01)
%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 two digits of year of ISO week number (see %G)
%G year of ISO week number (see %V); normally useful only with %V
%h same as %b
%H hour (00..23)
%I hour (01..12)
%j day of year (001..366)
%k hour ( 0..23)
%l hour ( 1..12)
%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 not known
%P like %p, but lower case
%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 a tab
%T time; same as %H:%M:%S
%u day of week (1..7); 1 is Monday
%U week number of year, with Sunday as first day of week (00..53)
%V ISO week number, with Monday as first day of week (01..53)
%w day of week (0..6); 0 is 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 timezone (e.g., -0400)
%:z +hh:mm numeric timezone (e.g., -04:00)
%::z +hh:mm:ss numeric time zone (e.g., -04:00:00)
%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30)
%Z alphabetic time zone abbreviation (e.g., EDT)
使用Linux date命令实例:
如果不指定选项,date命令将显示当前系统日期和时间,包括星期几,月份,时间,时区和年份:
$ date
Fri Sep 14 12:00:48 CST 2018
将当前时间按照'%Y-%m-%d %H:%M:%S'格式输出:
$ echo `date +'%Y-%m-%d %H:%M:%S'`
2018-09-14 13:38:42
指定-d选项,date命令将显示指定的特定时间:
$ date -d "1974-01-04"
Fri Jan 4 00:00:00 CST 1974
通过在格式字符串前面加上加号,date可是显示多种格式 :
$ date +"Week number: %V Year: %y"
Week number: 37 Year: 18
输出格式字符串,每个格式标记由其值替换。 %V是显示当前周数的格式选项,%y表示年份的最后两位数。
确定给定日期是周几:
$ date -d "1974-01-04" +"%A"
Friday
将当前时间按指定year-month-day格式输出:
$ echo `date "+%Y-%m-%d"`
2018-09-14
$ echo `date +%Y-%m-%d`
2018-09-14
将当前时间往前推30天的日期,按指定year-month-day格式输出:
$ echo `date -d "-30 day" "+%Y-%m-%d"`
2018-08-15
取当前时间前一天作为截止日期,截止日期往前推90天作为起始日期:
$ end_time=`date -d "-1 day" "+%Y-%m-%d"`
$ start_time=`date -d "$end_time -90 day" "+%Y-%m-%d"`
$ echo $start_time
2018-06-15
参考:https://www.linode.com/docs/tools-reference/tools/use-the-date-command-in-linux/#formatting-options