问题描述
我用Linux bash编写这些
I write these in Linux bash
date -d"2018-08-21 02:00:00" +'%y-%m-%d%T'
它会打印
2018-08-21 02:00:00
但是当我写这些
date -d "2018-08-21 02:00:00 +1 hour" +'%y-%m-%d %T'
它打印
2018-08-21 07:30:00
而不是 2018-08-21 03:00:00
它将我的时区添加到日期.添加时间单位时如何忽略时区?
It adds my timezone to the date. How can I ignore timezone when I'm adding time units?
推荐答案
正在发生的事情是 +1
被解释为时区 UTC + 1h
.因此,它将把输入日期从UTC + 1转换为本地时区,然后由于语句 hour
而仍然为其增加了一个小时.
What is happening is that the +1
is interpreted as the timezone UTC+1h
. So it will convert your input date from UTC+1 to your local time-zone and then still add an extra hour to it due to the statement hour
.
要解决此问题,您必须摆脱 +
符号.这里有一些可能性:
To solve this, you have to get rid of the +
sign. Here are some possibilities:
date -d "2018-08-21 02:00:00 next hour" "+%F %T"
date -d "2018-08-21 02:00:00 hour" "+%F %T"
或使用浮点数:
date -d "2018-08-21 02:00:00 + 1.0 hour" "+%F %T"
有关为什么会出现这种情况的更多信息,请查看:如何为存储在变量中的日期/时间添加间隔
For more information on why this is the case, have a look at:How to add an interval to a date/time stored in a variable
这篇关于将时间单位添加到没有时区的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!