转载请注明出处:

  在编写shell脚本时,需要在shell脚本中格式化时间,特此整理下date命令相关参数的应用

root@controller1:~# date --help
用法:date [选项]... [+格式]
 或: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'
--debug annotate the parsed date,
and warn about questionable usage to stderr
-f, --file=DATEFILE like --date; once for each line of DATEFILE
-I[FMT], --iso-8601[=FMT] output date/time in ISO 8601 format.
FMT='date' for date only (the default),
'hours', 'minutes', 'seconds', or 'ns'
for date and time to the indicated precision.
Example: 2006-08-14T02:34:56-06:00
-R, --rfc-email output date and time in RFC 5322 format.
Example: Mon, 14 Aug 2006 02:34:56 -0600
--rfc-3339=FMT output date/time in RFC 3339 format.
FMT='date', 'seconds', or 'ns'
for date and time to the indicated precision.
Example: 2006-08-14 02:34:56-06:00
-r, --reference=FILE display the last modification time of FILE
-s, --set=STRING set time described by STRING
-u, --utc, --universal print or set Coordinated Universal Time (UTC)
--help 显示此帮助信息并退出
--version 显示版本信息并退出 给定的格式FORMAT 控制着输出,解释序列如下: %% 一个文字的 %
%a 当前locale 的星期名缩写(例如: 日,代表星期日)
%A 当前locale 的星期名全称 (如:星期日)
%b 当前locale 的月名缩写 (如:一,代表一月)
%B 当前locale 的月名全称 (如:一月)
%c 当前locale 的日期和时间 (如:2005年3月3日 星期四 23:05:25)
%C 世纪;比如 %Y,通常为省略当前年份的后两位数字(例如:20)
%d 按月计的日期(例如:01)
%D 按月计的日期;等于%m/%d/%y
%e 按月计的日期,添加空格,等于%_d
%F 完整日期格式,等价于 %Y-%m-%d
%g ISO-8601 格式年份的最后两位 (参见%G)
%G ISO-8601 格式年份 (参见%V),一般只和 %V 结合使用
%h 等于%b
%H 小时(00-23)
%I 小时(00-12)
%j 按年计的日期(001-366)
%k hour, space padded ( 0..23); same as %_H
%l hour, space padded ( 1..12); same as %_I
%m month (01..12)
%M minute (00..59)
%n a newline
%N nanoseconds (000000000..999999999)
%p locale's equivalent of either AM or PM; blank if not known
%P like %p, but lower case
%q quarter of year (1..4)
%r locale's 12-hour clock time (e.g., 11:11:04 PM)
%R 24-hour hour and minute; same as %H:%M
%s seconds since 1970-01-01 00:00:00 UTC
%S 秒(00-60)
%t 输出制表符 Tab
%T 时间,等于%H:%M:%S
%u 星期,1 代表星期一
%U 一年中的第几周,以周日为每星期第一天(00-53)
%V ISO-8601 格式规范下的一年中第几周,以周一为每星期第一天(01-53)
%w 一星期中的第几日(0-6),0 代表周一
%W 一年中的第几周,以周一为每星期第一天(00-53)
%x 当前locale 下的日期描述 (如:12/31/99)
%X 当前locale 下的时间描述 (如:23:13:48)
%y 年份最后两位数位 (00-99)
%Y 年份
%z +hhmm 数字时区(例如,-0400)
%:z +hh:mm 数字时区(例如,-04:00)
%::z +hh:mm:ss 数字时区(例如,-04:00:00)
%:::z 数字时区带有必要的精度 (例如,-04,+05:30)
%Z 按字母表排序的时区缩写 (例如,EDT) 默认情况下,日期的数字区域以0 填充。
The following optional flags may follow '%': - (hyphen) do not pad the field
_ (underscore) pad with spaces
0 (zero) pad with zeros
^ use upper case if possible
# use opposite case if possible 在任何标记之后还允许一个可选的域宽度指定,它是一个十进制数字。
作为一个可选的修饰声明,它可以是E,在可能的情况下使用本地环境关联的
表示方式;或者是O,在可能的情况下使用本地环境关联的数字符号。 Examples:
Convert seconds since the epoch (1970-01-01 UTC) to a date
$ date --date='@2147483647' Show the time on the west coast of the US (use tzselect(1) to find TZ)
$ TZ='America/Los_Angeles' date Show the local time for 9AM next Friday on the west coast of the US
$ date --date='TZ="America/Los_Angeles" 09:00 next Fri'

  date命令获取3天以前时间,并格式化为utc时间:

root@controller1:~# date -u -d "3 days ago" +"%Y-%m-%dT%H:%M:%SZ"
2024-07-14T14:49:56Z

  shell脚本中应用示例:

#!/bin/bash  

# 使用date命令获取3天前的UTC时间,并格式化输出
# -d 选项允许你指定一个非当前的时间
# -u 选项表示输出UTC时间
# +"%Y-%m-%dT%H:%M:%SZ" 指定了输出的时间格式 three_days_ago=$(date -u -d "3 days ago" +"%Y-%m-%dT%H:%M:%SZ") echo "$three_days_ago"

linux date格式化获取时间的更多相关文章

  1. linux中用shell获取时间,日期

    linux中用shell获取昨天.明天或多天前的日期:在Linux中对man date -d 参数说的比较模糊,以下举例进一步说明:# -d, --date=STRING display time d ...

  2. linux date 格式化时间和日期

    [root@108test ~]# date -d today +"%Y-%m-%d" 2008-05-07   [root@108test ~]# date -d today + ...

  3. Date动态获取时间

    ·getDate            |  根据本地时间获取当前日期(本月的几号) ·getDay             |  根据本地时间获取今天是星期几(0-Sunday,1-Monday.. ...

  4. php date mktime 获取时间上的各种值

    echo date("Ymd",strtotime("now")), "\n"; echo date("Ymd",str ...

  5. linux date 设置系统时间

    设置 系统时间 注意时间格式 date  -s "date" [root@localhost c]# date -s "2019-05-29 10:58:00" ...

  6. linux下c获取时间

    头文件 #include "sys/time.h" 结构体 struct timezone { int tz_minuteswest; /*格林威治时间往西方的时差*/ int t ...

  7. linux c 编程 ------ 获取时间,计算程序执行时间

    #include <time.h> #include <stdio.h> #include <unistd.h> int main(int argc, char a ...

  8. javascript Date对象 之 获取时间

    javascript Date对象 --> 获取时间: 测试代码: <!DOCTYPE html> <html lang="en"> <head ...

  9. linux date

    我使用过的Linux命令之date - 显示.修改系统日期时间 本文链接:http://codingstandards.iteye.com/blog/1157513   (转载请注明出处) 用途说明 ...

  10. linux date使用

    Linux date 格式化时间和日期 [root@linuxidc ~]# date -d today +"%Y-%m-%d"  2016-11-26 [root@linuxid ...

随机推荐

  1. 在 Inno Setup iss 打包过程 中检测 .NET 6 / .net 5 / .NET Core 运行环境是否存在或已安装

    为了将 .NET 5 / .NET Core 应用程序部署到客户机,我们可以编写 Inno Setup 代码来判断客户机是否安装了必要的运行环境..NET 官方仓库 中提供了一个名为 NetCoreC ...

  2. .net Core中实现SHA加密

    #region 用SHA1加密字符串 /// <summary> /// 用SHA1加密字符串 /// </summary> /// <param name=" ...

  3. conda错误 创建新环境conda create -n TF117 python=3.5时报错 An unexpected error has occurred. Conda has prepared the above report.

    创建新环境conda create -n TF117 python=3.5时报错 An unexpected error has occurred. Conda has prepared the ab ...

  4. linux系统重要文件和目录说明

    系统信息相关文件 /etc/issue 记录操作系统版本 head /etc/issue /proc/cpuinfo 记录cpu信息 cat /proc/cpuinfo /proc/meminfo 记 ...

  5. makedown快速入门

    Makedown学习 Makedown 作为一个强大文本编辑语言,学习并熟悉应用是写好一篇优秀博客的基础 那么接下来我将介绍makedown语言最常用的几个语法 标题 +"space&quo ...

  6. rhce练习题容易错的地方

    rhce练习题里容易错的地方 使用导航器的时候,ssh连接 因为导航器是一个工具,生成一个容器,在容器里面运行playbook 安装软件包的时候,多个软件包使用循环loop loop的格式 - hos ...

  7. GeoGebra作圆的切线

    参考文档:<GeoGebra入门教程>唐家军 1. 目的 使用GeoGebra作出过一点的圆的切线. 2. 构造过程 文档种的描述如下: 按照上述构造过程,在输入条形框中依次输入上面的指令 ...

  8. mysql 判断字符串结尾

    mysql 判断字符串结尾 CREATE TABLE `tbl_str` ( `id` INT DEFAULT NULL, `Str` VARCHAR(30) DEFAULT NULL) INSERT ...

  9. UniRx-unirx中的对象池

    UniRx-unirx中的对象池 对象池Unirxunity 对象池 一.对象池模式 <游戏设计模式-对象池模式> 1.概念 定义一个池对象,其包含了一组可重用对象. 其中每个可重用对象都 ...

  10. httpx的使用

    urllib和requests库已经可以爬取大多数网站的数据,但对于一些强制使用HTTP/2.0协议访问,这时urllib和requests是无法爬取数据的,因为只支持HTTP/1.1,不支持HTTP ...