如何在 Bash 脚本中增加日期

编程入门 行业动态 更新时间:2024-10-27 16:31:33
本文介绍了如何在 Bash 脚本中增加日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 Bash 脚本,它接受格式为 yyyy-mm-dd 的日期参数.

I have a Bash script that takes an argument of a date formatted as yyyy-mm-dd.

我将其转换为秒

startdate="$(date -d"$1" +%s)";

我需要做的是迭代八次,每次将纪元日期增加一天,然后以 mm-dd-yyyy 格式显示.

What I need to do is iterate eight times, each time incrementing the epoch date by one day and then displaying it in the format mm-dd-yyyy.

推荐答案

使用 date 命令能够为现有日期添加天数.

Use the date command's ability to add days to existing dates.

以下内容:

DATE=2013-05-25 for i in {0..8} do NEXT_DATE=$(date +%m-%d-%Y -d "$DATE + $i day") echo "$NEXT_DATE" done

产生:

05-25-2013 05-26-2013 05-27-2013 05-28-2013 05-29-2013 05-30-2013 05-31-2013 06-01-2013 06-02-2013

请注意,这在您的情况下效果很好,但其他日期格式(例如 yyyymmdd)可能需要在日期字符串中包含UTC"(例如,date -ud20130515 UTC + 1 天").

Note, this works well in your case but other date formats such as yyyymmdd may need to include "UTC" in the date string (e.g., date -ud "20130515 UTC + 1 day").

更多推荐

如何在 Bash 脚本中增加日期

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

发布评论

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

>www.elefans.com

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