Invalid date error by date command
Solution 1
I'm almost sure this is due to the changeover to Daylight Saving Time in the given timezone: effectively this means that an hour "disappears" (and hence becomes "invalid
").
In my own timezone, DST started at 2AM on Sunday 10th March, so that hour is invalid:
$ cat /etc/timezone
America/Toronto
$ date --date="2019-03-10 02:00:00"
date: invalid date ‘2019-03-10 02:00:00’
whereas the times immediately before and after are valid:
$ date --date="2019-03-10 01:59:59"
Sun Mar 10 01:59:59 EST 2019
$ date --date="2019-03-10 03:00:00"
Sun Mar 10 03:00:00 EDT 2019
In timezones where the change over happens at midnight, the bare date appears invalid because GNU date
assumes a time of midnight:
$ TZ=Asia/Tehran date --date='2019-03-22'
date: invalid date ‘2019-03-22’
but one hour later is valid:
$ TZ=Asia/Tehran date --date='2019-03-22 01:00:00'
Fri Mar 22 01:00:00 +0430 2019
See also Invalid Date Linux
Solution 2
$ date_ascii="2019-03-22"
$ printf "%s" "$date_ascii" | od -c
0000000 2 0 1 9 - 0 3 - 2 2
0000012
$ TZ=Asia/Shanghai date -d "$date_ascii"
Fri Mar 22 00:00:00 America 2019
and
$ date_unicode="2019‑03‑22"
$ printf "%s" "$date_unicode" | od -c
0000000 2 0 1 9 342 200 221 0 3 342 200 221 2 2
0000016
$ TZ=Asia/Shanghai date -d "$date_unicode"
date: invalid date ‘2019‑03‑22’
Related videos on Youtube
![ICE](https://i.stack.imgur.com/lI5AP.jpg?s=256&g=1)
ICE
Updated on September 18, 2022Comments
-
ICE almost 2 years
I want to get the date information with this command:
date --date=2019-03-22
or
date --date=2019/03/22
but it shows this error:
date: invalid date ‘2019-03-22’
or
date: invalid date ‘2019/03/22’
as you can see it is not related to dash. the same thing happens with slash.
When I use another date like
date --date=2019-03-21
It shows the information correctly.
It shouldn't be related to the bad dash character. because I just deleted the last
2
and replaced it with1
and the output is OK.What is going wrong?
Result of some commands for more information:
$ date --version date (GNU coreutils) 8.28 Copyright (C) 2017 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by David MacKenzie.
$ type -a date date is /bin/date
$ uname -m x86_64
$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 18.04.2 LTS Release: 18.04 Codename: bionic
$ which date /bin/date
$ apt-cache policy coreutils coreutils: Installed: 8.28-1ubuntu1 Candidate: 8.28-1ubuntu1 Version table: *** 8.28-1ubuntu1 500 500 http://us.archive.ubuntu.com/ubuntu bionic/main amd64 Packages 100 /var/lib/dpkg/status
$ date Fri Mar 22 06:54:59 PDT 2019
date --date=2019-03-22 2>&1 | od -c 0000000 d a t e : i n v a l i d d a 0000020 t e 342 200 230 2 0 1 9 - 0 3 - 2 2 0000040 342 200 231 \n 0000044
Something weird going on with different timezone in this date:
2019-03-22
. I randomly changed timezone to different areas. Some of them have errors, some of them not! When I select these I have problem with that specific date:- Los Angeles (USA)
- Shanghai (China)
- Madrid (Spain)
-
ICE over 5 yearsSeems There is something wrong with timezone in my system :(
-
ICE over 5 yearsThanks, adding time to date fixed the problem.
-
fiatux over 5 yearsyou should reinstall the
tzdata
package. -
fiatux over 5 yearsThey "change to daylight saving at midnight" should not be a problem for 2019-03-22 as the users TZ is currently PDT.
-
ICE over 5 yearsreinstalling
tzdata
package fixed the problem for me. Now only one timezone has error as you explained in your answer without adding time. I don't know what went wrong withtzdata
package but evenTZ=America/Toronto date --date="2019-03-10 02:00:00"
returned the result without error before reinstalling thetzdata
package. thank you for your time.