13

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

Everything went well till it reached this date: 20130322

and then it returned this error:

date: invalid date ‘20130322 +1 day’

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?

14
  • 3
    It works on my system... Commented Oct 6, 2014 at 8:34
  • 2
    Show your full code. It works, just tried this script and stopped it at 20670201... Commented Oct 6, 2014 at 8:40
  • No problem with RHEL6, RHEL7, Ubuntu 11.04 and Ubuntu 14.04.1. Reproducible with RHEL5 but with date -d "20130101 +1 day" +%Y%m%d and date -d "20130322 +1 day" +%Y%m%d with date command from GNU coreutils version 5.97. Commented Oct 6, 2014 at 8:45
  • @jm666 I tried your code, still the same on CentOS 6.5 and Ubuntu 14.04 Commented Oct 6, 2014 at 9:12
  • Works with date (GNU coreutils) 8.23... strange. I never would have thought that there may be such bug.. Commented Oct 6, 2014 at 9:28

1 Answer 1

22

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.

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
Sign up to request clarification or add additional context in comments.

1 Comment

In my case, I had to add my time zone as well (UTC-3). So, what worked for me was: date -d "20170803 12:00 -0300 -1 day" +%Y%m%d

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.