英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论