Linux date invalid date

bash «date» returns «invalid date» error for a specific date string

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?

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.

Works with date (GNU coreutils) 8.23 . strange. I never would have thought that there may be such bug..

1 Answer 1

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 

Источник

Invalid Date Linux

I’m testing my backup script on Linux and when I try to set the date to ‘20141019’ I have this error:

# date +%Y%m%d -s "20141019" date: invalid date ‘20141019’ 
# date +%Y%m%d -s "20141018" 20141018 # date +%Y%m%d -s "20141020" 20141020 
# date -d '28 day ago' +%Y-%m-%d 2014-10-18 // at 20141117: # date -d '28 day ago' +%Y-%m-%d 2014-10-20 

3 Answers 3

Command: timedatectl set-time YYYY-MM-DD HH:MM:SS

Читайте также:  Jmeter запуск на linux

Set date and time: timedatectl set-time ‘2015-11-23 08:10:40’

Set only date: timedatectl set-time ‘2015-12-01’

Set only time: timedatectl set-time ’10:42:43′

It give you multiple solutions to set date and time, may be you can find something to solve your problem.

This is due to a problem with the timezone DST — if the timezone has entered the daylight saving time, then it won’t have the midnight to be counted on GNU date:

~$ TZ=America/Sao_Paulo date -d '20161016 + 1 day' date: invalid date `20161016 + 1 day' ~$ TZ=America/Sao_Paulo date -d '20171015 + 1 day' date: invalid date `20171015 + 1 day' ~$ TZ=America/Sao_Paulo date -d '20181021 + 1 day' date: invalid date `20181021 + 1 day' 

In this case, you can use UTC for that:

~$ TZ=UTC date -d '20171015 + 1 day' Mon Oct 16 00:00:00 UTC 2017 
~$ TZ=UTC date +%Y%m%d -d "20141019" 20141019 

Источник

date: invalid date trying to set linux date in specific format

I always just do MMDDhhmm, provided i don’t need to change the existing year. So for July 4 at 2:34pm you would do date 07041434 . If you wanted to set the year say to 1969, then it would be date 070414341969

if you do man date you will see that format defined such as [MMDDhhmm[[CC]YY][.ss]] The [ ] means optional. So if you did just date 1434 then you would be trying to set the month and day not the hour and minute. So if your date command requires MMDDhhmm then to simply set the time you always have to give 2-digit month and 2-digit day first before the 2-digit hour and 2-digit minute.

1 Answer 1

The issue looks like to be your «date string», i.e. «19-01-2017 00:05:01» .

The --date=STRING is a mostly free format human readable date string such as "Sun, 29 Feb 2004 16:21:42 -0800" or "2004-02-29 16:21:42" or even "next Thursday". A date string may contain items indicating calendar date, time of day, time zone, day of week, relative time, relative date, and numbers. 

Now, «a mostly free format human readable date» is a bit vague, and looks like 19-01-2017 is not readable. Probably because it might be unparseable in cases like 02-03-2019 (is it March, 02 or February, 03?).

Читайте также:  Linux назначить права только директориям

Try changing it to ISO format:

$> date "+%d-%m-%C%y %H:%M:%S" -d "2017-01-19 00:05:01" 19-01-2017 00:05:01 

o use a format like the example found in the man page:

$> date "+%d-%m-%C%y %H:%M:%S" -d "19 Jan 2017 00:05:01" 19-01-2017 00:05:01 

Источник

invalid date error in the date command linux

It gives error date: invalid date `+%Y%m%d’ what may be the reason. I know that the variable CURRENT_DATE have value in it.

3 Answers 3

date -d $CURRENT_DATE will print the date corresponding to the $CURRENT_DATE variable.

$) CURRENT_DATE="20140220" $) date -d $CURRENT_DATE Thu Feb 20 00:00:00 IST 2014 

To store the date into a variable, try using

$) CURRENT_DATE_TMP=`date +%Y%m%d` $) echo $CURRENT_DATE_TMP 20140704 

To print an existing date into a new format, use

$ CURRENT_DATE=`date +%Y-%m-%d` $ echo $CURRENT_DATE 2014-07-04 $ date -d$CURRENT_DATE "+%Y%m%d" 20140704 

Better still, wrap the $CURRENT_DATE variable within quotes, so that dates with spaces don’t break anything.

$ CURRENT_DATE=`date` $ echo $CURRENT_DATE Fri Jul 4 17:59:45 IST 2014 $ date -d"$CURRENT_DATE" "+%Y%m%d" 20140704 $ date -d$CURRENT_DATE "+%Y%m%d" date: extra operand ‘4’ 

In your current example, you have a space after the -d flag, remove it.

current date is not today’s date. It should be in the variable. Eg. CURRENT_DATE may have value 2014-03-10 in it.

i don’t want to store current date in variable. I have date in format YYYY-MM-DD in variable CURRENT_DATE and I want to get the date in format YYYYMMDD in variable CURRENT_DATE_tmp.

It looks like date -d «$CURRENT» «+%Y%m%d» works in bash; that is, if there are no spaces in $CURRENT, then either eliminating the space or putting $CURRENT in quotes will fix the problem. In any case, the answer (as edited) seems more than adequate to me.

@DavidK Yeah, Initially I thought the OP was trying to print date and had misread the flags, so I was thinking on those lines. It was when he clarified in the comments above that I realised what he was trying to do.

This is happening because the variable is unset or empty, and you did not quote the variable:

$ CURRENT_DATE="" $ CURRENT_DATE_tmp=$(date -d $CURRENT_DATE +%Y%m%d) date: invalid date ‘+%Y%m%d’ 

If you use quotes, no error:

$ CURRENT_DATE_tmp=$(date -d "$CURRENT_DATE" +%Y%m%d) $ echo $CURRENT_DATE_tmp 20140704 

This has already been mentioned in a comment, but you must make sure there is actually a value in the variable $CURRENT if you want to do what you are trying to do. Try inserting these two lines in your script:

echo date is =$CURRENT= date -d "$CURRENT" "+%Y%m%d" 

If one of the lines of output appears like the one below, then you have neglected to set $CURRENT to a non-empty string:

Читайте также:  Консоль линукса открыть флешку

Trying various variations of date -d «$CURRENT» «+%Y%m%d» (with or without quotes, with or without space after -d ) when $CURRENT is an empty string, either I see the «invalid date» error or I get today’s date.

Источник

date: invalid date ‘2018-10-21 +1 day’

Oh wait, you have the problem using the same date on one platform. I can’t reproduce the problem on my machine.

1 Answer 1

In Brasil, Summer Time starts at midnight. That’s different from most parts of the world, where the change is at 2:00 a.m., precisely because of the confusion which gives rise to this question.

NOTE: The following represents the information in the time zone file currently installed on my machine, and presumably that of the OP. However, in December, 2017, the government of Brasil decided to delay Summer Time by two weeks this year, so the transition will actually be on November 4, 2018. Hopefully, the TZ database will be updated before then.

Not all Brasilian states change their clocks twice a year. In at least one state (Matto Grosso), the decision is made by individual municipalities.

So in Sāo Paulo, there will be no 00:00:00 on October 21, 2018. When the clock ticks over a second from 23:59:59 on October 20, 2018, Summer Time will kick in and Sunday will start at 1 a.m.:

$ TZ=America/Sao_Paulo date -d "23:59:59 2018-10-20" Sat Oct 20 23:59:59 -03 2018 $ TZ=America/Sao_Paulo date -d "23:59:59 2018-10-20 +1 second" Sun Oct 21 01:00:00 -02 2018 

Note the change in TZ offset.

When you just type a date without a time, the time defaults to 0:00:00. If that time doesn’t exist on a given day, date complains:

$ TZ=America/Sao_Paulo date -d "2018-10-21" date: invalid date ‘2018-10-21’ $ TZ=America/Sao_Paulo date -d "00:00:00 2018-10-21" date: invalid date ‘00:00:00 2018-10-21’ $ TZ=America/Sao_Paulo date -d "00:30:00 2018-10-21" date: invalid date ‘00:30:00 2018-10-21’ 

Источник

Оцените статью
Adblock
detector