【问题标题】:How to find 30 days older epoch time in ms from current time in shell script?如何在 shell 脚本中找到比当前时间早 30 天的纪元时间?
【发布时间】:2021-08-09 06:47:57
【问题描述】:

如何在 shell 脚本中找到距当前时间 30 天的纪元时间?如果我当前的时间是以纪元毫秒为单位的 X。那么我需要 shell 脚本中 30 天前的纪元毫秒

#!/bin/sh
current=$(date +'%s%3N')
echo $current
oldtimestamp=$((current - 30*24*60*60))
echo "old is $oldtimestamp"

这似乎没有给出 30 天前的纪元毫秒。请让我知道如何获得它。提前致谢

【问题讨论】:

  • 您以秒为单位计算了 30 天;您需要乘以 1000 才能获得毫秒数。
  • 意思是:oldtimestamp=$((current - 30*24*60*60*1000))
  • 太好了,谢谢!

标签: bash shell timestamp epoch milliseconds


【解决方案1】:

date 命令支持一些自然语言表达式:

date  +'%s%3N' --date='30 days ago'

当然,与${current} 相比,这会减少几毫秒,因为执行这些命令本身需要时间,但也许对您来说已经足够接近了。

否则,正确的算术表达式是 Jonathan Leffler 写的:

oldtimestamp=$((current - 30*24*60*60*1000))

但请注意,像闰秒这样的事情可能会让你在这种方法中感到厌烦。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-11
    • 2021-12-01
    • 2013-03-14
    • 2021-08-10
    相关资源
    最近更新 更多