Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
share|improve this question
up vote 5 down vote accepted

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.
share|improve this answer
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. – Ketan 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 – Jagdeep 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. – Ketan Feb 18 '14 at 17:04

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:

share|improve this answer
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. – Ketan 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 :-) – Ketan Feb 18 '14 at 17:02

Your Answer


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.