用date命令输出timestamp

经常在写一些脚本时,需要在输出中打一些timestamp,却苦于不知如何格式化timestamp,在网上收集了一些利用date命令输出timestamp格式的方法。

YYYY-MM-DD_hh:mm:ssdate +%F_%T2018-07-30_17:38:48
YYYYMMDD_hhmmssdate +%Y%m%d_%H%M%S20180730_173848
YYYYMMDD_hhmmss (UTC version)date –utc +%Y%m%d_%H%M%SZ20180730_073848Z
YYYYMMDD_hhmmss (with local TZ)date +%Y%m%d_%H%M%S%Z20180730_173848AEST
YYYYMMSShhmmssdate +%Y%m%d%H%M%S20180730173848
YYYYMMSShhmmssnnnnnnnnndate +%Y%m%d%H%M%S%N20180730173848489146493
YYMMDD_hhmmssdate +%y%m%d_%H%M%S180730_173848
Seconds since UNIX epoch:date +%s1532936328
Nanoseconds only:date +%N490710955
Nanoseconds since UNIX epoch:date +%s%N1532936328491188547
ISO8601 UTC timestampdate –utc +%FT%TZ2018-07-30T07:38:48Z
ISO8601 UTC timestamp + msdate –utc +%FT%T.%3NZ2018-07-30T07:38:48.492Z
ISO8601 Local TZ timestampdate +%FT%T%Z2018-07-30T17:38:48AEST
YYYY-MM-DD (Short day)date +%F(%a)2018-07-30(Mon)
YYYY-MM-DD (Long day)date +%F(%A)2018-07-30(Monday)
  • :的不适合作为文件名,需要转义
  • 最后两条括号里的文字依赖于系统环境变量LANG,比如设置LANG=ja_JP会打印2018-07-30(月)2018-07-30(月曜日)
    原文作者:BetaRabbit
    原文地址: https://segmentfault.com/a/1190000015817430
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞