在 Bash 中使用 date 获取明天的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30235598/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:59:38  来源:igfitidea点击:

Using date to get tomorrows date in Bash

bashshellunix

提问by Cob50nm

I want to write a bash script that will run on a given but process data with next days date, My current approach is to get the unix time stamp and add a days worth of seconds to it, but I cant get it working, and haven't yet found what I'm looking for online.

我想编写一个 bash 脚本,该脚本将在给定的但处理下几天日期的数据上运行,我目前的方法是获取 unix 时间戳并为其添加几天的秒数,但我无法让它工作,并且没有还没有在网上找到我要找的东西。

Here's what I've tried, I feel like the problem is that its a string an not a number, but I dont know enough about bash to be sure, is this correct? and how do I resolve this?

这是我尝试过的,我觉得问题在于它是一个字符串而不是数字,但我对 bash 的了解不够确定,这是正确的吗?我该如何解决这个问题?

today="$(date +'%s')"

tomorrow="$($today + 86400)"

echo "$today"

echo "$tomorrow"

采纳答案by geirha

$(...)is command substitution. You're trying to run $today + 86400as a command.

$(...)是命令替换。您正在尝试$today + 86400作为命令运行。

$((...))is arithmetic expansion. This is what you want to use.

$((...))是算术展开式。这就是你想要使用的。

tomorrow=$(( today + 86400 ))

Also see http://mywiki.wooledge.org/ArithmeticExpressionfor more on doing arithmetics in the shell.

另请参阅http://mywiki.wooledge.org/ArithmeticExpression,了解有关在 shell 中进行算术的更多信息。

回答by anubhava

If you have gnu-datethen to get next day you can just do:

如果您必须gnu-date在第二天到达,您可以这样做:

date -d '+1 day'

回答by Doug Couvillion

Some of the answers for this question depend on having GNU date installed. If you don't have GNU date, you can use the built-in datecommand with the -voption.

这个问题的一些答案取决于安装了 GNU date。如果您没有 GNU 日期,则可以使用date带有-v选项的内置命令。

The command

命令

$ date -v+1d

$ date -v+1d

returns tomorrow's date.

返回明天的日期。

You can use it with all the standard dateformatting options, so

您可以将它与所有标准date格式选项一起使用,因此

$ date -v+1d +%Y-%m-%d

$ date -v+1d +%Y-%m-%d

returns tomorrow's date in the format YYYY-MM-DD.

以 YYYY-MM-DD 格式返回明天的日期。

回答by leAthlon

I hope that this will solve your problem here.

我希望这将在这里解决您的问题。

 date --date 'next day'

回答by Sobrique

Set your timezone, then run date.

设置您的时区,然后运行date.

E.g.

例如

TZ=UTC-24 date 

Alternatively, I'd use perl:

或者,我会使用 perl:

perl -e 'print localtime(time+84600)."\n"'

回答by Mircea Man

echo $(date --date="next day" +%Y%m%d)

This will output

这将输出

20170623

20170623

回答by suhas

You can try below

你可以试试下面

#!/bin/bash
today=`date`
tomorrow=`date --date="next day"`
echo "$today"
echo "$tomorrow"