首页 » Linux » CentOS » linux显示设置系统时间命令date详解

linux显示设置系统时间命令date详解

 
文章目录

显示设置系统时间命令date,在写shell脚本时经常会用到,date命令参数比较多,记住几个常用的即可!

date命令选项

-d 字符串:显示字符串所指的日期与时间,字符串前后必须加上双引号
-s 字符串:根据字符串来设置日期与时间,字符串前后必须加上双引号
-u:显示或设置通用时间时间域,UTC,CST,GMT
--help:帮助
--version:版本信息

date命令参数

%% 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) 显示年月日时间(例Mon 21 Nov 2016 11:15:40 AM CST )
%C century; like %Y, except omit last two digits (e.g., 20) 世纪
%d day of month (e.g, 01) 日期(01-31)
%D date; same as %m/%d/%y 年月日
%e day of month, space padded; same as %_d 日期(1-31)
%F full date; same as %Y-%m-%d 日期(和%Y-%m-%d一样)
%g last two digits of year of ISO week number (see %G) 只显示年的后两位16
%G year of ISO week number (see %V); normally useful only with %V 完整年份2016
%h same as %b 和%b一样
%H hour (00..23) 24小时制(00-23)
%I hour (01..12) 12小时制(01-12)
%j day of year (001..366) 一年第几天(001-366)
%k hour ( 0..23) 24小时制(0-23)
%l hour ( 1..12) 12小时制(1-12)
%m month (01..12) 月份(01-12)
%M minute (00..59) 分钟(00-59)
%n a newline 换行
%N nanoseconds (000000000..999999999) 纳秒(不明白的可以去搜纳秒)
%p locale's equivalent of either AM or PM; blank if not known AM或PM
%P like %p, but lower case am或pm
%r locale's 12-hour clock time (e.g., 11:11:04 PM) 显示时间12小时制
%R 24-hour hour and minute; same as %H:%M 显示时间24小时制
%s seconds since 1970-01-01 00:00:00 UTC 从1970年1月1日00:00:00到目前所经历的秒数
%S second (00..60) 秒(00-60)
%t a tab 制表符
%T time; same as %H:%M:%S 24小时制时间
%u day of week (1..7); 1 is Monday 一周的第几天,1就是星期一
%U week number of year, with Sunday as first day of week (00..53) 一年的第几周,周日为每周的第一天(00-53)
%V ISO week number, with Monday as first day of week (01..53) 一年的第几周,周一为每周的第一天(01-53)
%w day of week (0..6); 0 is Sunday 一周的第几天(0-6),0表示周日
%W week number of year, with Monday as first day of week (00..53) 一年的第几周,周一为每周的第一天(00-53)
%x locale's date representation (e.g., 12/31/99) 日期(mm/dd/yy)
%X locale's time representation (e.g., 23:13:48) 时间(%H:%M:%S)
%y last two digits of year (00..99) 年份(00-99)和%g一样
%Y year 见%G
%Z alphabetic time zone abbreviation (e.g., EDT) 时区

date几个示例

设置时间
date -s 11:28:23 设置时间为11点28分23秒,原来的年月日不变
设置所有时间
date -s "11:28:23 2016-10-10" 这样年月日也随之改变
设置年月
date -s 20161010 这样具体时间就变成了0点0分0秒
date -s "20120523 02:00:01"

七天前日期
date -d "7 day ago" +"%Y-%m-%d"
七天后日期
date -d "-7 day age" +"%Y-%m-%d"
上一月日期
date -d "-1 month" +"%Y-%m-%d"
下一月日期
date -d "1 month" +"%Y-%m-%d"
前一年日期
date -d "-1 year" +"%Y-%m-%d"
下一年日期
date -d "1 year" +"%Y-%m-%d"

原文链接:linux显示设置系统时间命令date详解,转载请注明来源!

2