0

i try to set linux date & time in specific format but it keep giving me error Example :

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

keep giving me this error :

date: invalid date ‘19-01-2017 00:05:01’

migrated from serverfault.com Jan 17 at 15:13

This question came from our site for system and network administrators.

  • 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 – ron Jan 17 at 16:47
  • 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. – ron Jan 17 at 16:50
1

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

From the man page:

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?).

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
  • The info pages (as recommended by the brief entry in the manpage) can expand on that "probably": when the month is a number, the date will be interpreted as either "year-month-day", or the US-style "month/day(/optional year)". – JigglyNaga Jan 17 at 16:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.