明天日期使用cat Bash如何获取?

huangapple go评论65阅读模式
英文:

How to get tommorow date using cat Bash?

问题

以下是翻译好的部分:

我知道我可以这样做:

    cat <<EOF
    现在是 $(date "+%Y年%m月%d日 %H:%M:%S UTC")
    EOF

这将输出:

    现在是2023年08月04日 17:40:00 UTC

我想要的是:

    现在是2023年08月04日 17:40:00 UTC
    明天是2023年08月05日 17:40:00 UTC

我尝试了以下方式:

    cat <<EOF
    现在是 $(date "+%Y年%m月%d日 %H:%M:%S UTC")
    明天是 $(date -d "+1 day" "+%Y年%m月%d日 %H:%M:%S UTC")
    EOF
    
但这不起作用... 它甚至不显示错误消息。
英文:

I know I can do:

cat &lt;&lt;EOF
Now is $(date &quot;+%B %d, %Y&quot;)
EOF

Which gives:

Now is August 04, 2023

What I want to get is:

Now is August 04, 2023 17:40 UTC
Tomorrow is August 05, 2023 17:40 UTC

I tried

cat &lt;&lt;EOF
Now is $(date &quot;+%B %d, %Y, %H:%s&quot;) UTC
Tomorrow is $(date -d &quot;+%B %d, %Y %H:%s&quot; &#39;+1 day&#39;) UTC
EOF

but this doesn't work.. it doesn't even show error message

答案1

得分: 1

The code snippet you provided is explaining how to use the date command in bash and suggests an alternative method using built-in time functionality in bash. Here's the translated code part:

`date` isn't part of bash, so bash can't control how it works for your OS. How `date` works for you is up to Apple, or Red Hat, or Ubuntu, or FreeBSD, or whoever else your OS vendor happens to be.

Consider instead using the time functionality that **is** built into bash (note that this was introduced in the 4.x series and isn't available in the ancient bash 3.2 that Apple ships by default):

```bash
printf -v now_epoch '%(%s)T' -1
printf 'Now is %(%B %d, %Y)T\n' "$now_epoch"
printf 'Tomorrow is %(%B %d, %Y)T\n' "$(( now_epoch + (60 * 60 * 24) ))"

Of course, you can embed this in a heredoc like anything else if you want cat to be between the shell-builtin printf and your stdout:

cat <<EOF
$(printf -v now_epoch '%(%s)T' -1
  printf 'Now is %(%B %d, %Y)T\n' "$now_epoch"
  printf 'Tomorrow is %(%B %d, %Y)T\n' "$(( now_epoch + (60 * 60 * 24) ))")
EOF

...though why it would make any sense to do so is beyond my comprehension.


Please note that I've translated the code part as requested, excluding any additional information or explanations.

<details>
<summary>英文:</summary>

`date` isn&#39;t  part of bash, so bash can&#39;t control how it works for your OS. How `date` works for you is up to Apple, or Red Hat, or Ubuntu, or FreeBSD, or whoever else your OS vendor happens to be.

Consider instead using the time functionality that **is** built in to bash (note that this was introduced in the 4.x series, and isn&#39;t available on the ancient bash 3.2 that Apple ships by default):

printf -v now_epoch '%(%s)T' -1
printf 'Now is %(%B %d, %Y)T\n' "$now_epoch"
printf 'Tomorrow is %(%B %d, %Y)T\n' "$(( now_epoch + (60 * 60 * 24) ))"


---

Of course you can embed this in a heredoc like anything else if you want `cat` to be between the shell-builtin `printf` and your stdout:

cat <<EOF
$(printf -v now_epoch '%(%s)T' -1
printf 'Now is %(%B %d, %Y)T\n' "$now_epoch"
printf 'Tomorrow is %(%B %d, %Y)T\n' "$(( now_epoch + (60 * 60 * 24) ))")
EOF


...though why it would make any sense to do so is beyond my comprehension.

</details>



# 答案2
**得分**: 1

以下是翻译好的部分:

```bash
你可以尝试使用以下命令

```bash
cat &lt;&lt;EOF
现在是 $(date &quot;+%Y年%m月%d日, %H:%s&quot;) UTC
明天是 $(date -v+1d &quot;+%Y年%m月%d日, %H:%s&quot; ) UTC
EOF

这里的 -v+1d 参数告诉 date 在当前日期上加上1天。


<details>
<summary>英文:</summary>

You can try using below

```bash
cat &lt;&lt;EOF
Now is $(date &quot;+%B %d, %Y, %H:%s&quot;) UTC
Tomorrow is $(date -v+1d &quot;+%B %d, %Y %H:%s&quot; ) UTC
EOF

Here -v+1d argument tells date to add 1 day to the current date.

huangapple
  • 本文由 发表于 2023年8月5日 03:22:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76838663.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定