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"

目录
相关文章
|
8天前
|
NoSQL Linux Shell
常用的 Linux 命令
常用的 Linux 命令
30 9
|
1天前
|
安全 Linux 开发工具
Linux中可引起文件时间戳改变的相关命令
【4月更文挑战第12天】Linux中可引起文件时间戳改变的相关命令
9 0
|
3天前
|
域名解析 网络协议 Linux
Linux 中的 Nslookup 命令怎么使用?
【4月更文挑战第12天】
22 6
Linux 中的 Nslookup 命令怎么使用?
|
3天前
|
运维 网络协议 Unix
18.系统知识-Linux常用命令
18.系统知识-Linux常用命令
|
4天前
|
网络协议 Ubuntu Linux
Linux 下 TFTP 服务搭建及 U-Boot 中使用 tftp 命令实现文件下载
Linux 下 TFTP 服务搭建及 U-Boot 中使用 tftp 命令实现文件下载
|
4天前
|
Linux Go
Linux命令Top 100驱动人生! 面试必备
探索Linux命令不再迷茫!本文分10部分详解20个基础命令,带你由浅入深掌握文件、目录管理和文本处理。 [1]: <https://cloud.tencent.com/developer/article/2396114> [2]: <https://pan.quark.cn/s/865a0bbd5720> [3]: <https://yv4kfv1n3j.feishu.cn/docx/MRyxdaqz8ow5RjxyL1ucrvOYnnH>
48 0
|
7天前
|
Linux
Linux 指令|date|cal|find|grep|热键
Linux 指令|date|cal|find|grep|热键
|
7天前
|
缓存 运维 监控
Linux系统监控利器:探索常用命令及数据保存技巧
Linux系统监控利器:探索常用命令及数据保存技巧
26 4
Linux系统监控利器:探索常用命令及数据保存技巧
|
8天前
|
Linux 数据安全/隐私保护
Linux设置PPPOE
请注意,以上步骤是基本的设置流程。具体步骤可能会因Linux发行版和版本的不同而有所差异,确保按照你所使用的系统来进行设置。如果使用图形界面,也可以在网络设置中配置PPPoE连接。 买CN2云服务器,免备案服务器,高防服务器,就选蓝易云。百度搜索:蓝易云
19 0
|
11天前
|
Web App开发 Linux 网络安全
工作中常用到的Linux命令
工作中常用到的Linux命令