打印特定格式的日期,提前5天。

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

Print date 5 days before in particular format

问题

I'm trying to get a particular format of a date that was 5 days ago. I'm running bash on MacOS.

Currently I have:

date -v -5d
Wed 1 Jan 2020 09:32:07 EST

But the format I'm looking for is:

2020-01-01

Looking at some solutions, I have something like:

DATE=$(date +"%Y%m%d")
date -j -f %Y%m%d -v-5d "${DATE}" +%Y-%m-%d
2020-01-01

Would this be the most optimal way to do this?

英文:

Im trying to get a particular format of a date that was 5 days ago.Im running bash on MacOS

Currently I have

date -v -5d
Wed  1 Jan 2020 09:32:07 EST

But the format Im looking for is

2020-01-01

Looking at some solutions I have something like

DATE=$(date +"%Y%m%d")
date -j -f %Y%m%d -v-5d "${DATE}" +%Y-%m-%d
2020-01-01

Would this be the most optimal way to do this ?

答案1

得分: 1

date -v -5d一样,无需提供明确的输入日期,以下命令也可以使用:

date -v -5d +%F  # %F是%Y-%m-%d的缩写

您只需添加所需的输出格式。

英文:

Just like date -v -5d works without providing an explicit input date, so will

date -v -5d +%F  # %F is short for %Y-%m-%d

You just need to add the desired output format.

答案2

得分: 1

你可以简单地使用以下命令打印日期:

date -v 5d +%m-%d-%Y

输出:01-05-2020

英文:

You can simply print the date with

date -v 5d +%m-%d-%Y
# output: 01-05-2020

huangapple
  • 本文由 发表于 2020年1月6日 22:37:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/59613950.html
匿名

发表评论

匿名网友

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

确定