I am creating script to check RTC working & for that first I am getting the date & time from user input in yyyy mm dd hh:mm:ss format.

read -p "Enter the date:" val
echo $val
date -s "${val}"

Now I am getting the error:

date: invalid date ‘2016 01 22 14:00

Please help in this. Thanks in advance.

  • 1
    Try yyyy-mm-dd hh:mm. – muru Jan 22 '16 at 9:09

From manpage:

       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.  An empty string indicates the
       beginning  of  the  day.   The  date string format is more complex than is easily documented here but is fully
       described in the info documentation.

So, yy-dd-mm hh:mm:ss formate should be preferred as input to date -s

Try with the following:

(unset -v IFS # restore IFS to default
read -p "Enter the date:" y m d time rest_ignored
date -s "$y-$m-$d $time")

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.