1

I want to know how to use shell convert from 01/Mar/2011 to 2011-03-01 on OS X?

in my bash:

bash-3.2$ date -d "03 Mar 2011" +%F
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

Thank @ghoti, on OS X should use like:

date -j -f '%d %b %Y' "02 JUN 2011" '+%F'
Failed conversion of ``02 JUN 2011'' using format ``%d %b %Y''
date: illegal time format
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

but, my system show me > date +%b 7, %b is a number, not an abbreviated month name.

Thanks.

axiaoxin
  • 308
  • 6
  • 22
  • 2
    Possible duplicate of [Convert date formats in bash](http://stackoverflow.com/questions/6508819/convert-date-formats-in-bash) – Tom Fenech Jul 07 '16 at 18:51
  • @TomFenech I tried on my bash `bash-3.2$ date -d'27 JUN 2011' +%Y%m%d` it show me: `usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]` – axiaoxin Jul 08 '16 at 02:38
  • 1
    @axiaoxin - what operating system are you using? The `date` command is actually not part of bash, it's a stand-alone utility whose usage depends on your operating system. From the usage you provide, it appears you might be using OS X, or another BSD (FreeBSD, NetBSD, etc). – ghoti Jul 08 '16 at 02:51
  • @ghoti yes, Im using OS X,thanks – axiaoxin Jul 08 '16 at 02:55
  • 1
    See my answer below. Note that the answer @TomFenech linked to is specific to ***GNU date***, commonly found in Linux. It's important to give us all the relevant information in your question, preferably both in the text AND in the tags. If you [fix this this question](http://stackoverflow.com/posts/38252899/edit), it might even qualify for an upvote rather than a close vote! – ghoti Jul 08 '16 at 02:59
  • @axiaoxin, what are you using to construct your command lines? Is it a text editor, perhaps? Might you be using en dashes (–, U+2013) rather than simple hyphens (-, U+002D)? They don't show up in your question, but this error could be produced by that. Alternately, can you verify that you are using simple single and double quotes, rather than the curly matching ones that might be introduced by a word processor? – ghoti Jul 08 '16 at 07:26
  • You must use the abbreviated month name according to the **current locale**. If the abbreviated month name is in another locale, specify the locale in the command. Examples with the US locale: `LC_TIME="en_US.UTF-8" date -j -f '%d %b %Y' "02 JUN 2016" '+%F'; date -j -f '%d/%b/%Y' "01/Mar/2011" '+%F'` – jackjr300 Jul 09 '16 at 15:34
  • If the month is a number, use `date -j -f '%d/%m/%Y' "01/03/2011" '+%F'` – jackjr300 Jul 09 '16 at 15:41
  • @jackjr300 Thanks, `export LC_TIME="en_US.UTF-8"` it solved – axiaoxin Jul 10 '16 at 05:10

2 Answers2

1

Using BSD date, you can convert one format to another by using the -f option.

For example, putting your input into a variable so you can see how to re-use this date command:

$ d="27 JUN 2011"
$ date -j -f '%d %b %Y' "$d" '+%F'
2011-06-27

You can man date to see how everything works, but the basics are this;

  • -j tells the command just to process input and not try to set your system clock.
  • -f this that uses this as an input format to interpret that.
  • +yadda uses the specified output format to print the interpreted date.

For details on the input format, on OS X or most BSDs, you can man strftime.

ghoti
  • 45,319
  • 8
  • 65
  • 104
  • on my system: > date +%b 7 – axiaoxin Jul 08 '16 at 03:06
  • > date -j -f '%d %b %Y' "02 JUN 2011" '+%F' Failed conversion of ``02 JUN 2011'' using format ``%d %b %Y'' date: illegal time format usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ... [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format] – axiaoxin Jul 08 '16 at 03:15
  • @axiaoxin ... Other than the comment I added to the question, I'm not sure what to tell you. If I copy the command from your comment into a terminal, it works perfectly for me in both Snow Leopard and El Capitan. What happens if YOU copy-and-paste from the comment you posted into a terminal? – ghoti Jul 08 '16 at 07:31
0

The idea is to convert date STRING to epoch time, then output it as the format you desire.

I tested it on Cygwin and CentOS.

convert date STRING to epoch time

use command 'date -d STRING +%s', STRING can be 01 Mar 2011 or 01-Mar-2011 or 01/03/2011

date -d '01 Mar 2011' +%s
1298908800

output epoch time to date format

use command 'date -d @EPOCH_TIME '+%Y-%m-%d'

date -d @1298908800 '+%Y-%m-%d'
2011-03-01

Because your date STRING 01/Mar/2011 is not a valid format for date -d, so you have to use 'sed' to convert it.

echo "01/Mar/2011"|sed -e 's/\// /g'
01 Mar 2011

So your solution can be

old_date="01/Mar/2011"

date_string=`echo "$old_date"|sed -e 's/\// /g'`

echo $date_string
01 Mar 2011

epoch_time=`date -d "$date_string" +%s`

echo $epoch_time
1298908800

date -d @$epoch_time '+%Y-%m-%d'
2011-03-01
Babu
  • 23
  • 4