失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > linux用date获取时间戳格式说明

linux用date获取时间戳格式说明

时间:2020-09-20 21:05:47

相关推荐

linux用date获取时间戳格式说明

我们在用户系统文件备份或者脚本运行方面会用掉时间戳标记,用以下方式可以获取时间戳

date --help可以获取参数

一、命令参数

-d<字符串>:显示字符串所指的日期与时间。字符串前后必须加上双引号;-s<字符串>:根据字符串来设置日期与时间。字符串前后必须加上双引号;-u:显示GMT;--help:在线帮助;--version:显示版本信息。

二、时间参数

标记颜色的是经常会用到的

%a locale's abbreviated weekday name (e.g., Sun) #星期几的简称( Sun..Sat)

%A locale's full weekday name (e.g., Sunday) #星期几的全称( Sunday..Saturday)

%b locale's abbreviated month name (e.g., Jan) #月的简称(Jan..Dec)

%B locale's full month name (e.g., January) #月的全称(January..December)

%c locale's date and time (e.g., Thu Mar 3 23:05:25 ) # 日期和时间(Mon 07 Feb 04:25:12 PM CST)

%C century; like %Y, except omit last two digits (e.g., 20) #类似%y,限制到前两位(20)

%d day of month (e.g., 01) #一个月的第几天(01..31)

%D date; same as %m/%d/%y #显示今天的日期日/月/年(02/07/22)

%e day of month, space padded; same as %_d #今天日期,“空格+日期”( 7)

%F full date; same as %Y-%m-%d #显示今天日期年-月-日 (-02-07)

%g last two digits of year of ISO week number (see %G) #显示今天年份保留后两位(22)

%G year of ISO week number (see %V); normally useful only with %V #显示今天年份

%h same as %b #月的简称,类似%b(Jan..Dec)

%H hour (00..23) #小时(以00-23来表示)。

%I hour (01..12) #小时(以01-12来表示)。

%j day of year (001..366) #该年中的第几天

%k hour, space padded ( 0..23); same as %_H #小时,和%H区别是个位没有0(以00-23来表示)。

%l hour, space padded ( 1..12); same as %_I #小时,和%I区别是个位没有0(以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) #时间+上午下午(04:45:52 PM)

%R 24-hour hour and minute; same as %H:%M #时间小时:分(16:46)

%s seconds since 1970-01-01 00:00:00 UTC 总秒数。起算时间为1970-01-01 00:00:00 UTC。

%S second (00..60) #秒(00-60)

%t a tab # 在显示时,插入tab

%T time; same as %H:%M:%S #时:分:秒(16:48:46)

%u day of week (1..7); 1 is Monday #星期数字表示(1-7)

%U week number of year, with Sunday as first day of week (00..53) #该年中的周数

%V ISO week number, with Monday as first day of week (01..53) #该年过了几个周一

%w day of week (0..6); 0 is Sunday #该周的天数,0代表周日,1代表周一,以此类推

%W week number of year, with Monday as first day of week (00..53) #该年过了几个周一

%x locale's date representation (e.g., 12/31/99) #日期月/天/年(02/07/)

%X locale's time representation (e.g., 23:13:48) #时间+上下午(04:55:02 PM)

%y last two digits of year (00..99) #显示今天年份保留后两位(22)

%Y year #年份()

%z +hhmm numeric time zone (e.g., -0400) #时区(+0800)

%:z +hh:mm numeric time zone (e.g., -04:00) #时区(+08:00)

%::z +hh:mm:ss numeric time zone (e.g., -04:00:00) #时区(+08:00:00)

%:::z numeric time zone with : to necessary precision (e.g., -04, +05:30) #时区(+08)

%Z alphabetic time zone abbreviation (e.g., EDT) #标准时区缩写(CST)

三、常用实例说明:

1、今天日期-02-07-17:03:38

date=`date "+%Y-%m-%d-%H:%M:%S"`

2、今天日期-02-07

today_date=`date "+%Y%m%d"`

3、昨天日期2026

yesterday_date=`date -d yesterday "+%Y%m%d"`

4、往前推30天的日期2028

old30_date=`date -d "30 days ago" "+%Y%m%d"`

需要大家经常操作熟练,多的不演示了,想要什么组合根据上面参数组合即可

如果觉得《linux用date获取时间戳格式说明》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。