5

I am using the date -d command to change a specific date format to another. Below is the example used

currDate=`Wed 12 Feb 2014`
formattedDate=`date -d"${currDate}" +%Y%m%d`
echo $formattedDate
9

You shouldn't use backticks (`) unless you are assigning the result of a command to a variable, in this case you are assigning a string so you should just quote it:

currDate="Wed 12 Feb 2014"
formattedDate=`date -d"${currDate}" +%Y%m%d`
echo $formattedDate

I don't have access to a mac so I can't test this but according to the OSX date man page, this should work:

formattedDate=`date -jf "%a %d %b %Y" "${currDate}" +%Y%m%d`

Many of the utilities in OSX are based on BSD versions of same so the info you find for Linux does not always translate to OSX. From man date on OSX:

 -f      Use input_fmt as the format string to parse the new_date provided
         rather than using the default [[[mm]dd]HH]MM[[cc]yy][.ss] format.

 -j      Do not try to set the date.  This allows you to use the -f flag in 
         addition to the + option to convert one date format to another.
8
  • It is not working in this form on OSX either. One issue I notice is -d which according to man is an option to specify dst. It needs -f to specify input format but that is not working either. – mkc Feb 18 '14 at 15:56
  • @Ketan sorry, I missed the OSX tag (in the future, please include the OS info in your question as well). see updated answer. – terdon Feb 18 '14 at 16:06
  • @terdon: Thanks for replying , but the alternative you gave is not working either – Learner Feb 18 '14 at 16:08
  • @Jagdeep you'll have to tweak it to find the right format. Read the manual page, or tell me what "not working" means exactly, do you get an error? A different format? Different date? You might need to change %d to %e for example. – terdon Feb 18 '14 at 16:09
  • This should be the accepted answer. I just updated mine after trying this for the record. – mkc Feb 18 '14 at 17:04
8

I tested the following on my OSX which worked:

currDate="Wed 12 Feb 2014"
formattedDate=`date -v"${currDate}" +%Y%m%d`
echo $formattedDate

From the manpage -v is:

Adjust (i.e., take the current date and display the result of the adjustment; not actually set the date) the second, minute, hour, month day, week day, month or year according to val. If val is preceded with a plus or minus sign, the date is adjusted forwards or backwards according to the remaining string, otherwise the relevant part of the date is set. The date can be adjusted as many times as required using these flags. Flags are processed in the order given.

This will get the right answer:

date -jf"%a %e %b %Y" "Wed 12 Feb 2014" +%Y%m%d

The output is:

20140212
4
  • Does that correctly display 20140212? If I read the man page correctly, it should just add to today's date unless you use -f. "Take the current date" – terdon Feb 18 '14 at 16:11
  • @terdon yeah it is not printing the desired vals. Not accepting the -f option either fiddling for 30 minutes now. – mkc Feb 18 '14 at 16:39
  • Should be close to what I posted dammit. The issue will be getting the input format right. You should need both -f and -j. – terdon Feb 18 '14 at 16:40
  • @terdon Absolutely! Updated my answer based on yours :-) – mkc Feb 18 '14 at 17:02

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.