Unable to use date command to change specific date format in Bash shell on OS X terminal

18,345

Solution 1

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.

Solution 2

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
Share:
18,345

Related videos on Youtube

Learner
Author by

Learner

I am learning Software programming with a goal to enhance my thinking level about the details of software so that I can provide a meaningful input/insight to the questions asked. I am interested to learn all languages but mainly focus on : Data Structure, Data Analysis, Data Storage, Java, Scripting. I have a vision to start free education school across the world where people learn advance methods of studying and teaching and teach many more to educate the world and free it from chains of poverty , negligence, hate-ism . I have a vision of the world where people do debate/discussion about development rather than cast/color or other physical fights.

Updated on September 18, 2022

Comments

  • Learner
    Learner over 1 year

    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
    
  • Ketan Maheshwari
    Ketan Maheshwari about 10 years
    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.
  • terdon
    terdon about 10 years
    @Ketan sorry, I missed the OSX tag (in the future, please include the OS info in your question as well). see updated answer.
  • Learner
    Learner about 10 years
    @terdon: Thanks for replying , but the alternative you gave is not working either
  • terdon
    terdon about 10 years
    @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
    terdon about 10 years
    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"
  • Ketan Maheshwari
    Ketan Maheshwari about 10 years
    @terdon yeah it is not printing the desired vals. Not accepting the -f option either fiddling for 30 minutes now.
  • terdon
    terdon about 10 years
    Should be close to what I posted dammit. The issue will be getting the input format right. You should need both -f and -j.
  • Ketan Maheshwari
    Ketan Maheshwari about 10 years
    @terdon Absolutely! Updated my answer based on yours :-)
  • Ketan Maheshwari
    Ketan Maheshwari about 10 years
    This should be the accepted answer. I just updated mine after trying this for the record.
  • Learner
    Learner about 10 years
    @terdon: after executing the command it shows me usage of date command , which I conclude that the date command format is not working ..
  • Learner
    Learner about 10 years
    @terdon : your second example worked i.e. by giving input format of date and output format , I am able to get the result Thanks