如何在Unix中找到当前日期减去7天?

编程入门 行业动态 更新时间:2024-10-10 11:26:53
本文介绍了如何在Unix中找到当前日期减去7天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试查找今天前7天的日期.

I am trying to find the date that was seven days before today.

CURRENT_DT=`date +"%F %T"` diff=$CURRENT_DT-7 echo $diff

我正在尝试类似上述的方法,以查找比当前日期少7天的时间.有人可以帮我吗?

I am trying stuff like the above to find the 7 days less than from current date. Could anyone help me out please?

推荐答案

GNU date将为您提供数学帮助:

GNU date will to the math for you:

date --date "7 days ago"

其他版本将要求您从UNIX时代开始就将当前日期转换为秒,手动减去7天的秒数,然后将其转换回所需的形式.请查阅您的date版本的文档,以获取有关如何在Unix时间戳之间进行转换的详细信息.这是再次使用GNU date的示例:

Other version will require you to covert the current date into seconds since the UNIX epoch first, manually subtract 7 days' worth of seconds, and convert that back into the desired form. Consult the documentation for your version of date for details on how to convert to and from Unix timestamps. Here's an example using GNU date again:

x=$(date +%s) x=$((x - 7 * 24 * 60 * 60)) date --date @$x

更多推荐

如何在Unix中找到当前日期减去7天?

本文发布于:2023-11-08 20:49:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570420.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   中找到   如何在   Unix

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!