在 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
Using date to get tomorrows date in Bash
提问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 + 86400
as 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-date
then 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 date
command with the -v
option.
这个问题的一些答案取决于安装了 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 date
formatting 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 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"