问题
I write these in Linux bash
date -d "2018-08-21 02:00:00" +'%y-%m-%d %T'
and it prints
2018-08-21 02:00:00
But when I write these
date -d "2018-08-21 02:00:00 +1 hour" +'%y-%m-%d %T'
it prints
2018-08-21 07:30:00
instead of 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:
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"
Or use float numbers:
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
来源:https://stackoverflow.com/questions/51948822/add-time-units-to-a-date-without-timezone