Date – How to Specify a Format for the Input to Date?

date

Given a date and time in a format that is not recognized by date, how can I get date to recognize the date and time?

For example:

$ date -d "09SEP2012:23:58:46"
date: invalid date `09SEP2012:23:58:46'
$ date -d "09SEP2012:23:58:46" --magic-option "ddMMMYYY:hh:mm:ss"
Sun Sep  9 23:58:46 MDT 2012

Does --magic-option exist? If not, is there a more elegant way to solve this rather than using sed to transform the input into a well-formed date string?

Best Answer

Neither POSIX nor GNU date have --magic-option. FreeBSD calls it -f (with a syntax similar to date's output format specifiers, not the one you propose).

Your date is very close to being recognized by GNU date: all it takes is replacing the colon that separates the date from the time by a space.

date -d "$(echo "09SEP2012:23:58:46" | sed 's/:/ /')"
Related Question