问题描述
我想使用 date
命令在bash中添加一天。日期的输入格式如下: 20130101 ,这意味着 2013年1月1日
I wanted to add one day to a date in bash using date
command. The input format of date is like this : 20130101 which means 01 January 2013
我使用此命令完成那个:
I use this command to accomplish that:
date -d "20130101 +1 day" +%Y%m%d
一切进展顺利,直到达到此日期: 20130322
Everything went well till it reached this date: 20130322
然后它返回此错误:
date: invalid date ‘20130322 +1 day’
我尝试了其他类似日期的代码,其中一些是好的,有些不是!是正常吗我的意思是说,它可能与某种程度上与数字系统转换有关,就像一个for循环到达第9个循环时发生的。如何正确解决问题?
I tried the code with some other similar dates, some of them were fine and some were not! Is it normal? I mean maybe it is somehow related to numeral system converting like the one that happens when a for loop reach 9th loop. How can I properly workaround the problem?
推荐答案
好的,我发现问题的原因。
Ok, I found the reason to the problem.
该问题与每个时区不同的夏令时相关。因此,错误预计会在不同的时间段对不同的时区进行复制。更多信息,请参见
The problem is related to daylight saving time which is different for every time zone. So the error is expected to reproduce at different dates in respect to different time zones. More information can be found here.
我的时区是IRST(+3:30),例如,在2013年3月22日(20130322)调整转发一小时,所以date命令返回此日期的无效日期错误。
为了解决这个问题,在提供的链接中也提到,你应该提供时间,除了日期,这显然不应该在无效时间的范围内。任何时间在22时前00:00:00至00:59:59之间的任何时间将对我的时区无效,应该避免。所以对于2013年3月22日,我可以改变命令,以避免错误:
My time zone is IRST (+3:30) that, for example, is adjusted to forward one hour on 22 March 2013 (20130322), so the date command returns "Invalid Date" error for this date.For solving the problem, as also mentioned in the provided link, you should provide time in addition of date, which is obviously should not be in the range of invalid time. Any time between 00:00:00 to 00:59:59 on 22 march will be invalid for my time zone and should be avoided. So for 22 March 2013 I can change the command this way to avoid the error:
date -d "20130322 12:00 +1 day" +%Y%m%d
这篇关于bash“date”返回“无效日期”特定日期字符串的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!