在 Bash 中获取日期(当前时间前一天)

发布于 2024-08-10 04:04:36 字数 30 浏览 5 评论 0 原文

如何在 Bash 中打印当前时间前一天的日期?

How can I print the date which is a day before current time in Bash?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

橙幽之幻 2024-08-17 04:04:36

如果你有 GNU 日期并且我理解正确

$ date +%Y:%m:%d -d "yesterday"
2009:11:09

或者

$ date +%Y:%m:%d -d "1 day ago"
2009:11:09

if you have GNU date and i understood you correctly

$ date +%Y:%m:%d -d "yesterday"
2009:11:09

or

$ date +%Y:%m:%d -d "1 day ago"
2009:11:09
把人绕傻吧 2024-08-17 04:04:36

如果您有 BSD (OSX) date 您可以这样做:

date -j -v-1d
Wed Dec 14 15:34:14 CET 2011

或者如果您想在任意日期进行日期计算:

date -j -v-1d -f "%Y-%m-%d" "2011-09-01" "+%Y-%m-%d"
2011-08-31

If you have BSD (OSX) date you can do it like this:

date -j -v-1d
Wed Dec 14 15:34:14 CET 2011

Or if you want to do date calculations on an arbitrary date:

date -j -v-1d -f "%Y-%m-%d" "2011-09-01" "+%Y-%m-%d"
2011-08-31
醉南桥 2024-08-17 04:04:36
date --date='-1 day'
date --date='-1 day'
自由如风 2024-08-17 04:04:36

MAC OSX

对于昨天的日期:

date -v-1d +%F

其中 1d 定义当天减去 1 天。同样,

date -v-1w +%F - 表示上周日期

date -v-1m +%F - 表示上个月日期

如果您有 GNU DATE ,

date --date="1 day ago"

更多信息:https://www .cyberciti.biz/tips/linux-unix-get-yesterdays-tomorrows-date.html

MAC OSX

For yesterday's date:

date -v-1d +%F

where 1d defines current day minus 1 day. Similarly,

date -v-1w +%F - for previous week date

date -v-1m +%F - for previous month date

IF YOU HAVE GNU DATE,

date --date="1 day ago"

More info: https://www.cyberciti.biz/tips/linux-unix-get-yesterdays-tomorrows-date.html

等风来 2024-08-17 04:04:36

高级 Bash 脚本指南

date +%Y:%m:%d -d "yesterday"

有关日期格式的详细信息,请参阅 < a href="http://ss64.com/bash/date.html" rel="noreferrer">日期

date --date='-1 day'

Advanced Bash-scripting Guide

date +%Y:%m:%d -d "yesterday"

For details about the date format see the man page for date

date --date='-1 day'
薆情海 2024-08-17 04:04:36

抱歉没有提到我在 Solaris 系统上的情况。
因此,-date 开关在 Solaris bash 上不可用。

我发现我可以通过时区上的小技巧来获取之前的日期。

DATE=`TZ=MYT+16 date +%Y-%m-%d_%r`
echo $DATE

Sorry not mentioning I on Solaris system.
As such, the -date switch is not available on Solaris bash.

I find out I can get the previous date with little trick on timezone.

DATE=`TZ=MYT+16 date +%Y-%m-%d_%r`
echo $DATE
我一直都在从未离去 2024-08-17 04:04:36

好吧,这是一个迟到的答案,但这似乎有效!

     YESTERDAY=`TZ=GMT+24 date +%d-%m-%Y`;
     echo $YESTERDAY;

Well this is a late answer,but this seems to work!!

     YESTERDAY=`TZ=GMT+24 date +%d-%m-%Y`;
     echo $YESTERDAY;
束缚m 2024-08-17 04:04:36
date -d "yesterday" '+%Y-%m-%d'

或者

date=$(date -d "yesterday" '+%Y-%m-%d')
echo $date
date -d "yesterday" '+%Y-%m-%d'

or

date=$(date -d "yesterday" '+%Y-%m-%d')
echo $date
简美 2024-08-17 04:04:36

或许可以使用 Perl 来代替?

perl -e 'print scalar localtime( time - 86400 ) . "\n";'

或者,使用 nawk 和(ab)使用 /usr/bin/adb:

nawk 'BEGIN{printf "0t%d=Y\n", srand()-86400}' | adb

遇到 这也...疯了!

/usr/bin/truss /usr/bin/date 2>&1 | nawk -F= '/^time\(\)/ {gsub(/ /,"",$2);printf "0t%d=Y\n", $2-86400}' | adb

Use Perl instead perhaps?

perl -e 'print scalar localtime( time - 86400 ) . "\n";'

Or, use nawk and (ab)use /usr/bin/adb:

nawk 'BEGIN{printf "0t%d=Y\n", srand()-86400}' | adb

Came across this too ... insane!

/usr/bin/truss /usr/bin/date 2>&1 | nawk -F= '/^time\(\)/ {gsub(/ /,"",$2);printf "0t%d=Y\n", $2-86400}' | adb
空宴 2024-08-17 04:04:36

<代码> <代码>

date --date='-1 day'

>

date --date='-1 day'

山川志 2024-08-17 04:04:36

不是很性感,但可能可以完成工作:

perl -e 'my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time - 86400);$year += 1900; $mon+= 1; printf ("YESTERDAY: %04d%02d%02d \n", $year, $mon, $mday)'

由“马丁·克莱顿”答案组成。

Not very sexy but might do the job:

perl -e 'my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time - 86400);$year += 1900; $mon+= 1; printf ("YESTERDAY: %04d%02d%02d \n", $year, $mon, $mday)'

Formated from "martin clayton" answer.

不弃不离 2024-08-17 04:04:36

简短回答(GNU 格式):

date +%Y-%m-%d -d "-2 day"

如果您使用 OSX,但需要创建 GNU 兼容版本,请先安装 coreutils,

brew install coreutils

然后编辑您的配置文件:

#gnu coreutils first
export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"

重新启动终端,现在您可以使用 GNU 格式!

short answer (GNU format):

date +%Y-%m-%d -d "-2 day"

if you are using OSX, but you need create for GNU compatible, install coreutils first

brew install coreutils

then edit your profile with:

#gnu coreutils first
export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"

re-start your terminal, and now you able to use GNU format!

夜空下最亮的亮点 2024-08-17 04:04:36

如果选择的日期格式是“YYYYMM”,您可以使用正则表达式进行简单的计算:

echo $(($(date +"%Y%m") - 1)) | sed -e 's/99$/12/'

2020 年 1 月它将返回 201912 ;-)
但是,这只是一个解决方法,当日期没有计算选项并且其他日期解释器选项(例如使用 perl)不可用时;-)

You could do a simple calculation, pimped with an regex, if the chosen date format is 'YYYYMM':

echo $(($(date +"%Y%m") - 1)) | sed -e 's/99$/12/'

In January of 2020 it will return 201912 ;-)
But, it's only a workaround, when date does not have calculation options and other dateinterpreter options (e.g. using perl) not available ;-)

╄→承喏 2024-08-17 04:04:36
yesterday=`date -d "-1 day" %F`

将昨天的日期(YYYY-MM-DD 格式)放入变量 $yesterday 中。

yesterday=`date -d "-1 day" %F`

Puts yesterday's date in YYYY-MM-DD format into variable $yesterday.

划一舟意中人 2024-08-17 04:04:36
#!/bin/bash
OFFSET=1;
eval `date "+day=%d; month=%m; year=%Y"`
# Subtract offset from day, if it goes below one use 'cal'
# to determine the number of days in the previous month.
day=`expr $day - $OFFSET`
if [ $day -le 0 ] ;then
month=`expr $month - 1`
if [ $month -eq 0 ] ;then
year=`expr $year - 1`
month=12
fi
set `cal $month $year`
xday=${$#}
day=`expr $xday + $day`
fi
echo $year-$month-$day
#!/bin/bash
OFFSET=1;
eval `date "+day=%d; month=%m; year=%Y"`
# Subtract offset from day, if it goes below one use 'cal'
# to determine the number of days in the previous month.
day=`expr $day - $OFFSET`
if [ $day -le 0 ] ;then
month=`expr $month - 1`
if [ $month -eq 0 ] ;then
year=`expr $year - 1`
month=12
fi
set `cal $month $year`
xday=${$#}
day=`expr $xday + $day`
fi
echo $year-$month-$day
柠檬色的秋千 2024-08-17 04:04:36

DST 感知解决方案:

可以通过操纵时区来更改时钟几个小时。由于夏令时的原因,24小时前可以是今天或前天。

您确定昨天是 20 或 30 小时前。哪一个?好吧,最近的一个不是今天。

echo -e "$(TZ=GMT+30 date +%Y-%m-%d)\n$(TZ=GMT+20 date +%Y-%m-%d)" | grep -v $(date +%Y-%m-%d) | tail -1

bash 需要 echo 命令中使用的 -e 参数,但不适用于 ksh。在 ksh 中,您可以使用不带 -e 标志的相同命令。

当您的脚本将在不同的环境中使用时,您可以使用#!/bin/ksh 或#!/bin/bash 启动脚本。您还可以用换行符替换 \n:

echo "$(TZ=GMT+30 date +%Y-%m-%d)
$(TZ=GMT+20 date +%Y-%m-%d)" | grep -v $(date +%Y-%m-%d) | tail -1

DST aware solution:

Manipulating the Timezone is possible for changing the clock some hours. Due to the daylight saving time, 24 hours ago can be today or the day before yesterday.

You are sure that yesterday is 20 or 30 hours ago. Which one? Well, the most recent one that is not today.

echo -e "$(TZ=GMT+30 date +%Y-%m-%d)\n$(TZ=GMT+20 date +%Y-%m-%d)" | grep -v $(date +%Y-%m-%d) | tail -1

The -e parameter used in the echo command is needed with bash, but will not work with ksh. In ksh you can use the same command without the -e flag.

When your script will be used in different environments, you can start the script with #!/bin/ksh or #!/bin/bash. You could also replace the \n by a newline:

echo "$(TZ=GMT+30 date +%Y-%m-%d)
$(TZ=GMT+20 date +%Y-%m-%d)" | grep -v $(date +%Y-%m-%d) | tail -1
大海や 2024-08-17 04:04:36

尝试下面的代码,它也处理 DST 部分。

if [ $(date +%w) -eq $(date -u +%w) ]; then
  tz=$(( 10#$gmthour - 10#$localhour ))
else
  tz=$(( 24 - 10#$gmthour + 10#$localhour ))
fi
echo $tz
myTime=`TZ=GMT+$tz date +'%Y%m%d'`

考特西 安斯加·维彻斯

Try the below code , which takes care of the DST part as well.

if [ $(date +%w) -eq $(date -u +%w) ]; then
  tz=$(( 10#$gmthour - 10#$localhour ))
else
  tz=$(( 24 - 10#$gmthour + 10#$localhour ))
fi
echo $tz
myTime=`TZ=GMT+$tz date +'%Y%m%d'`

Courtsey Ansgar Wiechers

青春有你 2024-08-17 04:04:36

对于我的情况,我需要一个便携式解决方案来获取自一小时前的纪元以来的秒数,这在 ubuntu 和 macOS 上都有效:

$(( $(date +%s) - 3600 ))

解释一下:

  • $(( ))正在做算术扩展
  • date +%s 似乎是一种可移植的方式来获取
  • 一小时内自纪元 3600 秒

以来的秒数希望这有帮助!

For my case I needed a portable solution to get the number of seconds since the epoch one hour ago, and this worked on both ubuntu and macOS:

$(( $(date +%s) - 3600 ))

Explaining this:

  • $(( )) is doing an arithmetic expansion
  • date +%s seems to be a portable way to get seconds since epoch
  • 3600 seconds in an hour

Hope this helps!

初懵 2024-08-17 04:04:36
date +%Y:%m:%d|awk -vFS=":" -vOFS=":" '{$3=$3-1;print}'
2009:11:9
date +%Y:%m:%d|awk -vFS=":" -vOFS=":" '{$3=$3-1;print}'
2009:11:9
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文