基本使用格式:
date [-d "time-to-display"] +"format-to-display"
-d指定要显示的时间,如果不指定默认为当前时间;后边的+后的字符串为指定的输出格式,类式C的print的格式。常用示例如下:
date -d "1 day ago" +"%Y-%m-%d"
date -d "30 day ago" +"%Y-%m-%d"
date -d "+ 1 day" +"%Y-%m-%d"
date -d "- 1 month" +"%Y-%m-%d"
date -d "" +"%Y-%m-%d"
date -d "" +"输出为:%Y-%m-%d"
支持的输出项:
%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 :: ) %C century; like %Y, except omit last two digits (e.g., ) %d day of month (e.g, ) %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 (..) %I hour (..) %j day of year (..) %k hour ( ..)
%l hour ( ..) %m month (..) %M minute (..) %n a newline %N nanoseconds (..) %p locale’s equivalent of either AM or PM; blank if not known %P like %p, but lower case %r locale’s -hour clock time (e.g., :: PM) %R -hour hour and minute; same as %H:%M %s seconds since -- :: UTC %S second (..) %t a tab %T time; same as %H:%M:%S %u day of week (..); is Monday %U week number of year, with Sunday as first day of week (..) %V ISO week number, with Monday as first day of week (..) %w day of week (..); is Sunday %W week number of year, with Monday as first day of week (..) %x locale’s date representation (e.g., //) %X locale’s time representation (e.g., ::) %y last two digits of year (..) %Y year %z +hhmm numeric timezone (e.g., -) %:z +hh:mm numeric timezone (e.g., -:) %::z +hh:mm:ss numeric time zone (e.g., -::) %:::z numeric time zone with : to necessary precision (e.g., -, +:) %Z alphabetic time zone abbreviation (e.g., EDT)