linux命令总结之date命令
命令简介:
date 根据给定格式显示日期或设置系统日期时间。print or set the system date and time
指令所在路径:/bin/date
命令语法:
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]]
命令参数:
|
参数 |
描述 |
|
-d |
显示字符串描述的时间 |
|
-f |
显示DATEFILE文件中的每行时间 |
|
-r |
显示文件的最后修改时间 |
|
-R |
以RFC-2822兼容日期格式显示时间 |
|
-rfc-2822 |
以RFC-2822兼容日期格式显示时间 |
|
-s |
设置时间为string |
|
-u |
显示或设定为Coordinated Universal Time时间格式 |
|
--help |
显示date命令的帮助信息 |
|
--version |
显示date命令的版本信息 |
Format参数格式
要说写这位程序的 David MacKenzie老兄,真是事无巨细啊,居然整了这么多格式参数,佩服佩服。
|
参数 |
描述 |
|
%% |
显示字符% |
|
%a |
星期几的缩写(Sun..Sat) |
|
%A |
星期几的完整名称(Sunday...Saturday) |
|
%b |
月份的缩写(Jan..Dec) |
|
%B |
月份的完整名称(January..December) |
|
%c |
日期与时间。只输入date指令也会显示同样的结果 |
|
%C |
世纪(年份除100后去整) [00-99] |
|
%d |
日期(以01-31来表示)。 |
|
%D |
日期(含年月日)。 |
|
%e |
一个月的第几天 ( 1..31) |
|
%F |
日期,同%Y-%m-%d |
|
%g |
年份(yy) |
|
%G |
年份(yyyy) |
|
%h |
同%b |
|
%H |
小时(00..23) |
|
%I |
小时(01..12) |
|
%j |
一年的第几天(001..366) |
|
%k |
小时( 0..23) |
|
%l |
小时( 1..12) |
|
%m |
月份(01..12) |
|
%M |
分钟(00..59) |
|
%n |
换行 |
|
%N |
纳秒(000000000..999999999) |
|
%p |
AM or PM |
|
%P |
am or pm |
|
%r |
12小时制时间(hh:mm:ss [AP]M) |
|
%R |
24小时制时间(hh:mm) |
|
%s |
从00:00:00 1970-01-01 UTC开始的秒数 |
|
%S |
秒(00..60) |
|
%t |
制表符 |
|
%T |
24小时制时间(hh:mm:ss) |
|
%u |
一周的第几天(1..7); 1 表示星期一 |
|
%U |
一年的第几周,周日为每周的第一天(00..53) |
|
%V |
一年的第几周,周一为每周的第一天 (01..53) |
|
%w |
一周的第几天 (0..6); 0 代表周日 |
|
%W |
一年的第几周,周一为每周的第一天(00..53) |
|
%x |
日期(mm/dd/yy) |
|
%X |
时间(%H:%M:%S) |
|
%y |
年份(00..99) |
|
%Y |
年份 (1970…) |
|
%z |
RFC-2822 风格数字格式时区(-0500) |
|
%Z |
时区(e.g., EDT), 无法确定时区则为空 |
使用示例:
1: 查看date命令帮助信息
[root@Gin scripts]# 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- output date and time in RFC format.
Example: Mon, Aug :: -
--rfc-=TIMESPEC output date and time in RFC format.
TIMESPEC=`date', `seconds', or `ns' for
date and time to the indicated precision.
Date and time components are separated by
a single space: -- ::-:
-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., )
%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 12-hour clock time (e.g., 11:11:04 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., 12/31/99)
%X locale's time representation (e.g., 23:13:48)
%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) By default, date pads numeric fields with zeroes.
The following optional flags may follow `%': - (hyphen) do not pad the field
_ (underscore) pad with spaces
(zero) pad with zeros
^ use upper case if possible
# use opposite case if possible After any flags comes an optional field width, as a decimal number;
then an optional modifier, which is either
E to use the locale's alternate representations if available, or
O to use the locale's alternate numeric symbols if available. Report date bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'date invocation'
date --help
[root@Gin scripts]# man date
DATE() User Commands DATE() NAME
date - print or set the system date and time SYNOPSIS
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]] DESCRIPTION
Display the current time in the given FORMAT, or set the system
:
DATE() User Commands DATE() NAME
date - print or set the system date and time SYNOPSIS
date [OPTION]... [+FORMAT]
date [-u|--utc|--universal] [MMDDhhmm[[CC]YY][.ss]] DESCRIPTION
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-
output date and time in RFC format. Example: Mon,
Aug :: - --rfc-=TIMESPEC
output date and time in RFC format. TIME-
SPEC=‘date’, ‘seconds’, or ‘ns’ for date and time to the
indicated precision. Date and time components are sepa-
rated by a single space: -- ::-: -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 :: ) %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) By default, date pads numeric fields with zeroes. The following
optional flags may follow ‘%’: - (hyphen) do not pad the field _ (underscore) pad with spaces (zero) pad with zeros ^ use upper case if possible # use opposite case if possible After any flags comes an optional field width, as a decimal num-
ber; then an optional modifier, which is either E to use the
locale’s alternate representations if available, or O to use the
locale’s alternate numeric symbols if available. DATE STRING
The --date=STRING is a mostly free format human readable date
string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29
::" or even "next Thursday". A date string may contain
items indicating calendar date, time of day, time zone, day of
week, relative time, relative date, and numbers. An empty
string indicates the beginning of the day. The date string for-
mat is more complex than is easily documented here but is fully
described in the info documentation. ENVIRONMENT
TZ Specifies the timezone, unless overridden by command line
parameters. If neither is specified, the setting from
/etc/localtime is used. AUTHOR
Written by David MacKenzie. REPORTING BUGS
Report date bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/core-
utils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report date translation bugs to <http://translationpro-
ject.org/team/> COPYRIGHT
Copyright © Free Software Foundation, Inc. License GPLv3+:
GNU GPL version or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute
it. There is NO WARRANTY, to the extent permitted by law. SEE ALSO
The full documentation for date is maintained as a Texinfo man-
ual. If the info and date programs are properly installed at
your site, the command info coreutils 'date invocation' should give you access to the complete manual. GNU coreutils 8.4 October DATE()
(END)
man date
2:运用-d参数
[root@Gin scripts]# date
Sun Jan 29 10:46:03 CST 2017
[root@Gin scripts]# date -d now
Sun Jan 29 10:46:07 CST 2017
[root@Gin scripts]# date -d 'next monday'
Mon Jan 30 00:00:00 CST 2017
[root@Gin scripts]# date -d yesterday +%Y%m%d
20170128
3:显示文件中的时间
[root@Gin scripts]# cat >date.txt
2013-11-17 10:54:00
2013-11-22 11:11:11 [root@Gin scripts]# more date.txt
2013-11-17 10:54:00
2013-11-22 11:11:11 [root@Gin scripts]# date -f date.txt
Sun Nov 17 10:54:00 CST 2013
Fri Nov 22 11:11:11 CST 2013
4:显示文件最后修改的时间
[root@Gin scripts]# date -r date.txt
Sun Jan 29 10:49:11 CST 2017
5:按各种格式显示当前日期时间
这个命令的格式参数实在是太多了,在此没有必要每一个参数都尝试一遍,大家可以对照Format参数表,自己敲一敲命令实践一下。
[root@Gin scripts]# date +%Y
2017
[root@Gin scripts]# date +%m
01
[root@Gin scripts]# date +%D
01/29/17
[root@Gin scripts]# date '+%Y-%m-%d'
2017-01-29
[root@Gin scripts]# date +%Y-%m-%d
2017-01-29
[root@Gin scripts]# date +%m/%d/%y
01/29/17
6: 设置系统时间
[root@Gin scripts]# date -s "2016-11-11 00:00:00"
Fri Nov 11 00:00:00 CST 2016
[root@Gin scripts]# date
Fri Nov 11 00:00:05 CST 2016
7:请给出如下格式的date命令,如:11-02-26。再给出实现按周输出,如:周六为6,请分别给出命令
[root@Gin scripts]# date +%y-%m-%d
17-01-29
[root@Gin scripts]# date +%F
2017-01-29
[root@Gin scripts]# date "+%Y-%m-%d %H:%M:%S"
2017-01-29 10:57:02
生产场景中常用时间格式来打包数据:
[root@Andy andy]# tar zcvf etc-$(date +%F).tar.gz /etc
[root@Andy andy]# tar zcvf etc-`date +%F`.tar.gz /etc
解析命令方法:反引号,或$(),如上
[root@Andy andy]# date +%w ##显示周,0-6
3
显示过去与未来时间:
[root@Andy andy]# date +%F
2016-11-16
[root@Andy andy]# date +%F -d "-1day"
2016-11-15
[root@Andy andy]# date +%F -d "+2day"
2016-11-18
[root@Andy andy]# date +%F -d "+24hour"
2016-11-17
[root@Andy andy]# date +%F-%H -d "+2hour"
2016-11-16-14
linux命令总结之date命令的更多相关文章
- linux修改系统时间date命令加clock -w
http://m.jb51.net/LINUXjishu/117784.html 修改linux系统时间的方法(date命令) 11-18 23:22:27作者:脚本之家 命令格式为: date -s ...
- shell脚本删除N天前的目录-----附linux和mac上date命令的不同
背景: 每日构建的东西.按日期放到不同的目录里. 现在天的构建放到2015-06-01里,明天的就放到2015-06-02里,依次类推.时间久了.须要一个脚本删除N天前的目录.(本例中N=7.即删除一 ...
- 每天一个linux命令(37):date命令
在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数 ...
- Linux下date命令,格式化输出,时间设置
date命令的帮助信息 [root@localhost source]# date --help用法:date [选项]... [+格式] 或:date [-u|--utc|--universal] ...
- linux系统date命令详解
Linux时钟分为系统时钟(System Clock)和硬件(Real Time Clock,简称RTC)时钟.系统时钟是指当前Linux Kernel中的时钟,而硬件时钟则是主板上由电池供电的时钟, ...
- Linux date命令的用法
在linux shell编程中,经常用到日期的加减运算以前都是自己通过expr函数计算,很麻烦.其实date命令本身提供了日期的加减运算非常方便. 例如:得到昨天的时间date --date=&qu ...
- Linux命令学习-date
date命令可以用来显示和修改系统日期时间,注意不是time命令. 1.在命令行输入date显示当前时间 [root@vm4 logs]# dateSat Nov 22 00:00:02 CST 20 ...
- 每天一个linux命令(29):date命令
在linux环境中,不管是编程还是其他维护,时间是必不可少的,也经常会用到时间的运算,熟练运用date命令来表示自己想要表示的时间,肯定可以给自己的工作带来诸多方便. 1.命令格式: date [参数 ...
- 修改linux系统时间的方法(date命令)
修改linux系统时间的方法(date命令) 来源:互联网 作者:佚名 时间:11-18 23:22:27 [大 中 小] date命令不仅可以显示系统当前时间,还可以用它来修改系统时间,下面简单的介 ...
随机推荐
- 微信公众号开发笔记1(nodejs开发)
本篇记录了微信公众号开发的一些笔记 一.微信服务器与我们服务器的交流 微信开发者拥有自己的服务器,在我们服务器上可以与微信服务器进行交流.既然可以交流,那就必定需要前提条件(微信认证),也就是说,只有 ...
- stateful openflow------整理openstate原理以及具体应用
openstate基本思想就是控制器下放一部分功能,交换机不再是简单的dumb,而是保留一些简单的wise. 论文中以端口锁定为例,提出了米粒型状态机在交换机内部的应用从而可以大大减少交换机和控制器之 ...
- Leetcode题库——24.两两交换链表中的节点
@author: ZZQ @software: PyCharm @file: swapPairs.py @time: 2018/10/20 19:49 说明:给定一个链表,两两交换其中相邻的节点,并返 ...
- 第一次spring冲刺第8天
针对这几天出现的问题,我们团队做了用户需求讨论. 1.客户类型:工作者为主,其他类型都适用的计算器软件 2.需求与满足:他们想要的是能使用简单,并且适用于工作上 3.满足度:最好后台可以提供意见反馈, ...
- 韩剧TV APP案例分析
产品 选择产品:韩剧TV 版本:Android版 选择理由:节假日坐车回家时使用较多次数的APP,刚好国庆坐车回家时正在使用,所以选择了这款APP. 第一部分:调研.评测 第一次上手体验 刚打开APP ...
- 19_集合_第19天(List、Set)_讲义
今日内容介绍 1.List接口 2.Set接口 3.判断集合唯一性原理 非常重要的关系图 xmind下载地址 链接:https://pan.baidu.com/s/1kx0XabmT27pt4Ll9A ...
- UIPickerView的使用
简介:UIPickerView是一个选择器控件,它比UIDatePicker更加通用,它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活.UIPick ...
- beta发布的评论
1. 组名:飞天小女警 项目名:礼物挑选小工具 评价:对于我们学生来说,选礼物小工具相对来说新颖,小组添加了前十名热门礼物的推荐.发布到服务器上了,未来也有很多选择的空间可以做成礼物挑选手机app,也 ...
- 移动端-webkit-user-select:none导致input/textarea输入框无法输入
这个问题,也算是个大坑了. 最开始的开始,是因为我们在做大装盘活动的时候,发现在ios上面出现了这样的问题:点击“转”按钮,ios上面会有延迟并且会出现图片的阴影,这个肯定就不好看了撒,然后,找吧,改 ...
- jsp页面has already been called for this response错误解决方法。
创建验证码的jsp页面提示错误:has already been called for this response <%@ page contentType="image/jpeg&q ...