当前位置:   article > 正文

【亲测】Shell日期时间和时间戳相互转化_shell时间格式

shell时间格式

先说一下为什么写这篇文章,因为目前没有一篇文章能让我在Mac上成功执行的脚本。

date -d 在Mac上提示以下错误:

date: option requires an argument -- d
usage: date [-jnRu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
  • 1
  • 2
  • 3

以下时间戳都是以秒为单位

  • 自定义日期时间转时间戳
#/bin/sh
help="?"
if [ $# != 1 ] ; then
  echo "参数为空,输入的时间格式为:2022-01-16 15:26:11"
  exit 0
elif [[ $1 = "?" ]]; then
  echo "输入的时间格式为:2022-01-16 15:26:11"
  exit 0
fi
echo "北京时间:"$1
echo "时间戳:" $(date -j -f "%Y-%m-%d %H:%M:%S" "$1" +%s)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 时间戳转日期
#/bin/sh
echo "北京时间:"$(date -r $1)
echo "时间戳:"$1
  • 1
  • 2
  • 3
本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/914182
推荐阅读
相关标签
  

闽ICP备14008679号