计算两个日期之间的天数。

huangapple go评论49阅读模式
英文:

Bash calculate days in two date

问题

这个命令是有效的(给定的日期是一个字符串):

daysToExpiry=$((($(date +%s --date "Oct 15 05:17:34 2023 GMT")-$(date +%s))/(3600*24)))

但是,使用变量 $expiry 时出现了错误 "date: extra operand ‘15’":

daysToExpiry=$((($(date +%s --date $expiry)-$(date +%s))/(3600*24)))

$expiry 的值是 "Oct 15 05:17:34 2023 GMT"。我不清楚使用变量 $expiry 出现错误的原因是什么?

英文:

I am new to bash. I have a command to calculate the days between given date and current date.

This one is working (The given date is a sring):

daysToExpiry=$((($(date +%s --date "Oct 15 05:17:34 2023 GMT")-$(date +%s))/(3600*24)))

But, I got error "date: extra operand ‘15’" with this command (the same date string in a variable):

daysToExpiry=$((($(date +%s --date $expiry)-$(date +%s))/(3600*24)))

$expiry is "Oct 15 05:17:34 2023 GMT". I am wondering what's wrong with the using variable $expiry?

Thanks!

答案1

得分: 2

知道了,我应该使用 ""$expiry""

英文:

Figured out, I should use "$expiry"

huangapple
  • 本文由 发表于 2023年7月20日 17:29:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76728488.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定