如何在MacOS上的shell脚本中将DATE转换为UNIX TIMESTAMP

Dav*_*ung 72 linux macos shell date

在Linux上,您可以将"2010-10-02"之类的日期转换为shell脚本中的unix时间戳

date -d "2010-10-02" "+%s"
Run Code Online (Sandbox Code Playgroud)

由于Mac OS没有等效的-d作为日期.如何在shell脚本中将日期转换为unix时间戳.

小智 131

date +%s
Run Code Online (Sandbox Code Playgroud)

这对我在OS X Lion上运行正常.

  • 也适用于Linux(Ubuntu 10.04) (9认同)
  • OP询问将特定给定日期转换为时间戳.`date +%s`将只使用当前日期. (2认同)

Lou*_*nco 31

man date 在OSX上有这个例子

date -j -f "%a %b %d %T %Z %Y" "`date`" "+%s"
Run Code Online (Sandbox Code Playgroud)

我觉得你想做什么.

您可以将此用于特定日期

date -j -f "%a %b %d %T %Z %Y" "Tue Sep 28 19:35:15 EDT 2010" "+%s"
Run Code Online (Sandbox Code Playgroud)

或者使用您想要的任何格式.


小智 15

date -j -f "%Y-%m-%d" "2010-10-02" "+%s"