Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

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/:/ /')"
share|improve this answer
add comment

I wrote a bunch of tools (dateutils) that deal with dates and times in a more script-friendly way. Your magic option there is --input-format|-i, e.g.:

dconv -i '%d%b%Y:%H:%M:%S' "09SEP2012:23:58:46"
=>
  2012-09-09T23:58:46

While dconv does not directly support date's output format (it doesn't confer TZ or anything in the environment), there's a tool strptime in dateutils that does support the %Z format specifier.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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