shell编程中date用法(转)

原文地址:http://blog.sina.com.cn/s/blog_61c006ea0100mgxe.html

1、date --help

%% 输出%符号 a literal %
%a 当前域的星期缩写 locale’s abbreviated weekday name (Sun..Sat)
%A 当前域的星期全写 locale’s full weekday name, variable length (Sunday..Saturday)
%b 当前域的月份缩写 locale’s abbreviated month name (Jan..Dec)
%B 当前域的月份全称 locale’s full month name, variable length (January..December)
%c 当前域的默认时间格式 locale’s date and time (Sat Nov 04 12:02:33 EST 1989)
%C n百年 century (year divided by 100 and truncated to an integer) [00-99]
%d 两位的天 day of month (01..31)
%D 短时间格式 date (mm/dd/yy)
%e 短格式天 day of month, blank padded ( 1..31)
%F 文件时间格式 same as %Y-%m-%d
%g the 2-digit year corresponding to the %V week number
%G the 4-digit year corresponding to the %V week number
%h same as %b
%H 24小时制的小时 hour (00..23)
%I 12小时制的小时 hour (01..12)
%j 一年中的第几天 day of year (001..366)
%k 短格式24小时制的小时 hour ( 0..23)
%l 短格式12小时制的小时 hour ( 1..12)
%m 双位月份 month (01..12)
%M 双位分钟 minute (00..59)
%n 换行 a newline
%N 十亿分之一秒 nanoseconds (000000000..999999999)
%p 大写的当前域的上下午指示 locale’s upper case AM or PM indicator (blank in many locales)
%P 小写的当前域的上下午指示 locale’s lower case am or pm indicator (blank in many locales)
%r 12小时制的时间表示(时:分:秒,双位) time, 12-hour (hh:mm:ss [AP]M)
%R 24小时制的时间表示 (时:分,双位)time, 24-hour (hh:mm)
%s 自基础时间 1970-01-01 00:00:00 到当前时刻的秒数 seconds since `00:00:00 1970-01-01 UTC’ (a GNU extension)
%S 双位秒 second (00..60); the 60 is necessary to accommodate a leap second
%t 横向制表位(tab) a horizontal tab
%T 24小时制时间表示 time, 24-hour (hh:mm:ss)
%u 数字表示的星期(从星期一开始 1-7)day of week (1..7); 1 represents Monday
%U 一年中的第几周星期天为开始 week number of year with Sunday as first day of week (00..53)
%V 一年中的第几周星期一为开始 week number of year with Monday as first day of week (01..53)
%w 一周中的第几天 星期天为开始 0-6 day of week (0..6); 0 represents Sunday
%W 一年中的第几周星期一为开始 week number of year with Monday as first day of week (00..53)
%x 本地日期格式 locale’s date representation (mm/dd/yy)
%X 本地时间格式 locale’s time representation (%H:%M:%S)
%y 两位的年 last two digits of year (00..99)
%Y 年 year (1970…)
%z RFC-2822 标准时间格式表示的域 RFC-2822 style numeric timezone (-0500) (a nonstandard extension)
%Z 时间域 time zone (e.g., EDT), or nothing if no time zone is determinable

By default, date pads numeric fields with zeroes. GNU date recognizes
the following modifiers between `%’ and a numeric directive.

`-’ (hyphen) do not pad the field
`_’ (underscore) pad the field with spaces

--------------------------------------------------------------------------------

2、一些用法

1)#以yymmdd的格式输出43天前的当前时刻

date +%Y%m%d --date='43 days ago'       

 

2)# 测试十亿分之一秒
date +’%Y%m%d %H:%M:%S.%N’;date +’%Y%m%d %H:%M:%S.%N’;date +’%Y%m%d %H:%M:%S.%N’;date +’%Y%m%d %H:%M:%S.%N’

3)#创建以当前时间为文件名的目录
mkdir `date +%Y%m%d`

 

4)#备份以时间做为文件名的
tar -cvf ./htdocs`date +%Y%m%d`.tar ./*

 

5)#显示时间后跳行,再显示目前日期 

date +%T%n%Y%m%d

 

6)#只显示月份与日数 

date +%B%d

 

7)#获取上周日期(day,month,year,hour)

date -d "-1 week" +%Y%m%d   

 

8)#获取24小时前日期

date --date="-24 hour" +%Y%m%d

 

9)#shell脚本里面赋给变量值

date_now=`date +%s`

 

10)#计算执行一段sql脚本的运行时间

 

TIME_BEGIN=$(date '+%s.%N')
$sqlcli < queries/q1.3.sql 1>> $FILE_RESULT  2>> $FILE_ERROR
TIME_END=$(date '+%s.%N')
TIME_RUN=$(awk 'BEGIN{print '$TIME_END' - '$TIME_BEGIN'}')

 

11)#编写shell脚本计算离自己生日还有多少天?

    read -p "Input your birthday(YYYYmmdd):" date1

  m=`date --date="$date1" +%m`    #得到生日的月

  d=`date --date="$date1" +%d`    #得到生日的日

  date_now=`date +%s`             #得到当前时间的秒值

  y=`date +%Y`                    #得到当前时间的年

  birth=`date --date="$y$m$d" +%s`      #得到今年的生日日期的秒值

  internal=$(($birth-$date_now))        #计算今日到生日日期的间隔时间

  if [ "$internal" -lt "0" ]; then             #判断今天的生日是否已过

  birth=`date --date="$(($y+1))$m$d" +%s`      #得到明天的生日日期秒值

  internal=$(($birth-$date_now))               #计算今天到下一个生日的间隔时间

  fi

  echo "There is :$((einternal/60/60/24)) days."       #输出结果,秒换算为天

 

 

12)#若是不以加号作为开头,则表示要设定时间,而时间格式为 MMDDhhmm[[CC]YY][.ss],

其中 MM 为月份,

DD 为日,

hh 为小时,

mm 为分钟,

CC 为年份前两位数字,

YY 为年份后两位数字,

ss 为秒数

 

13)

#显示目前的格林威治时间,也叫“世界时”。是英国的标准时间,也是世界各地时间的参考标准。中英两国的标准时差为8个小时,即英国的当地时间比中国的北京时间晚8小时。

date -u              
Thu Sep 28 09:32:04 UTC 2006

 

14)#修改时间

date -s
按字符串方式修改时间
可以只修改日期,不修改时间,输入: date -s 2007-08-03
只修改时间,输入:date -s 14:15:00
同时修改日期时间,注意要加双引号,日期与时间之间有一空格,输入:date -s "2007-08-03 14:15:00"

修改完后,记得输入:clock -w
把系统时间写入CMOS

转载于:https://www.cnblogs.com/axeprpr/p/5131634.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/news/458405.shtml
繁体地址,请注明出处:http://hk.pswp.cn/news/458405.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux下搭建FTP服务器

LINUX FTP简单配置 FTP配置1、#vi /etc/vsftp/vsftpd.conf #主要配置几个关键的就可以 anonymous_enableNO #拒绝匿名访问 chroot_local_userYES #锁定用户目录&#xff0c…

微信正则表达式 iOS

#pragma mark - 微信号的正则表达式 微信账号仅支持6-20个字母、数字、下划线或减号&#xff0c;以字母开头 (BOOL)isWxNumber:(NSString *)WXNum{ NSString *passWordRegex "^[a-zA-Z][a-zA-Z0-9_-]{5,19}$"; NSPredicate *passWordPredicate [NSPredica…

子元素增加margin-top会增加给父元素的问题

假设我们有如下代码 <div id"father" style"height:400px;width:400px;background:#e4393c;">     <div id"child" style"background:green;height:100px;width:100px;margin-top:40px;"></div>   </div&g…

Python 关键字

from&#xff1a;https://blog.csdn.net/liang19890820/article/details/68488392简述 关键字是预先保留的标识符&#xff0c;每个关键字都有特殊的含义。编程语言众多&#xff0c;但每种语言都有相应的关键字&#xff0c;Python 也不例外&#xff0c;它自带了一个 keyword 模块…

Zend Studio使用教程之升级Zend Studio(1/3)

2019独角兽企业重金招聘Python工程师标准>>> Zend Studio是新一代的专业级智能PHP IDE&#xff0c;它旨在帮助开发人员提高工作效率&#xff0c;创造出高品质的PHP应用程序&#xff01;它包含了PHP开发所必须的部件&#xff0c;通过一整套的编辑、调试、分析、优化和…

身份证正则表达式

#pragma mark - 身份证的正则表达式 (BOOL)checkUserID:(NSString *)userID { //长度不为18的都排除掉 if (userID.length!18) { return NO; } //校验格式 NSString *regex2 "^(^[1-9]\\d{7}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d…

java中可重入锁的学习总结

2019独角兽企业重金招聘Python工程师标准>>> 经常看到网上的人说&#xff0c;可重入锁一词&#xff0c;但是总是没怎么了解&#xff0c;到底什么是可重入锁&#xff0c;一直是一个模糊的概念&#xff0c;下面来大致总结一下。 可重入锁&#xff1a;指的是同一个线程…

Oracle DB优化

http://www.jb51.net/article/77876.htm http://www.jb51.net/article/56881.htm http://danni505.blog.51cto.com/15547/1163711/ http://blog.csdn.net/giianhui/article/details/8172786转载于:https://www.cnblogs.com/diyunpeng/p/5132392.html

设置View单个圆角

#pragma mark - 设置View单个圆角 (void)addCornerWithView:(UIView *)aView type:(UIRectCorner)aCorners size:(CGSize)aSize { // 根据矩形画带圆角的曲线 UIBezierPath *maskPath [UIBezierPath bezierPathWithRoundedRect:aView.bounds byRoundingCorners:aCorn…

Python 数据类型

简述Python 中的每个值都有一个数据类型。在 Python 编程中&#xff0c;一切&#xff08;万物&#xff09;皆对象&#xff0c;数据类型实际上是类&#xff0c;变量是这些类的实例&#xff08;对象&#xff09;。简述数据类型Number数字String字符串List列表Tuple元组Set集合Dic…

基于用户投票的排名算法(一):Delicious和Hacker News

互联网的出现&#xff0c;意味着"信息大爆炸"。 用户担心的&#xff0c;不再是信息太少&#xff0c;而是信息太多。如何从大量信息之中&#xff0c;快速有效地找出最重要的内容&#xff0c;成了互联网的一大核心问题。 各种各样的排名算法&#xff0c;是目前过滤信息…

iOS 修改工程名

一两个月之前&#xff0c;公司要求将现在的项目&#xff08;发货端和接单端在一个项目里&#xff09;&#xff0c;拆分成两个项目分别是接单端项目和发货端项目&#xff0c;原有的项目还不能下架。这种情况就要考虑苹果审核查代码的重复率的问题了。老板的要求除了改变项目的主…

Windows 下 Python 环境搭建

简述Python 是跨平台的&#xff0c;可以运行在 Windows、Mac OS X 和各种 Linux/Unix 系统上。在学习 Python 之前&#xff0c;首先要搭建 Python 环境。完成后&#xff0c;会得到 Python 解释器&#xff08;负责运行 Python 程序的&#xff09;&#xff0c;一个命令行交互环境…

面试中关于Java你所需知道的的一切

本篇文章会对面试中常遇到的Java技术点进行全面深入的总结&#xff0c;帮助我们在面试中更加得心应手&#xff0c;不参加面试的同学也能够借此机会梳理一下自己的知识体系&#xff0c;进行查漏补缺。 1. Java中的原始数据类型都有哪些&#xff0c;它们的大小及对应的封装类是什…

利用BBRSACryptor实现iOS端的RSA加解密

背景 RSA这种非对称加密被广泛的运用于网络数据的传输&#xff0c;但其在iOS上很难直接实现&#xff0c;BBRSACryptor框架通过移植openssl实现了iOS端的RSA&#xff0c;本文将介绍如何使用BBRSACryptor生成证书&#xff0c;加载公钥&#xff0c;以及后端如何用php读取证书&…

UIView转UIimage

/** 将 UIView 转换成 UIImage param view 将要转换的View return 新生成的 UIImage 对象 */ - (UIImage *)yj_convertCreateImageWithUIView:(UIView *)view{ UIGraphicsBeginImageContext(view.bounds.size); CGContextRef ctx UIGraphicsGetCurrentContext…

Linq 合并数据并相加

有几条数据是这样的 Person 123 456 789 Person 321 654 987 想合并成 Person 444 1110 1776 直接一条linq搞定 var newQuery from p in query group p by p.Name into gselect new { Name g.Name, Value g.Sum(x > x.Value) }; 转…

python 各种模块学习

from&#xff1a;https://blog.csdn.net/weiwangchao_/article/details/70570508转载&#xff1a;。。。。Python的模块大全&#xff0c;很全&#xff0c;有详细介绍&#xff01;另外附Python两个教程1. Python详细教程&#xff08;廖雪峰的官方网站&#xff0c;语言简洁&#…

Linux(Fedora21)安装google chrome浏览器

2019独角兽企业重金招聘Python工程师标准>>> Linux(Fedora21)安装Google Chrome浏览器 qianghaoaho(孤狼) 1.添加google chrome的源&#xff1a; cd /etc/yum.repos.d/ vim chrome.repo添加如下内容&#xff1a; [google64] …

启动页更换图片后,加载不出来

这个问题&#xff0c;重启一下手机就可以了&#xff0c;我的就是这么解决的。