英文:
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 <<EOF
Now is $(date "+%B %d, %Y")
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 <<EOF
Now is $(date "+%B %d, %Y, %H:%s") UTC
Tomorrow is $(date -d "+%B %d, %Y %H:%s" '+1 day') 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'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 in to bash (note that this was introduced in the 4.x series, and isn'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 <<EOF
现在是 $(date "+%Y年%m月%d日, %H:%s") UTC
明天是 $(date -v+1d "+%Y年%m月%d日, %H:%s" ) UTC
EOF
这里的 -v+1d
参数告诉 date
在当前日期上加上1天。
<details>
<summary>英文:</summary>
You can try using below
```bash
cat <<EOF
Now is $(date "+%B %d, %Y, %H:%s") UTC
Tomorrow is $(date -v+1d "+%B %d, %Y %H:%s" ) UTC
EOF
Here -v+1d
argument tells date
to add 1 day to the current date.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论