1

I have one problem with my date format. I want to change from one format to the other and vice verse. My date formats are:

  • Format1 : 1/24/2014
  • Format2 : Jan 24

How can I do this?

7

Using GNU date. From format 1 to 2:

LC_ALL=C date --date=1/24/2014 +"%b %-d"              
Jan 24

And from 2 to 1:

date --date="Jan 24" +"%-m/%-d/%Y"
1/24/2014
1

Using ksh93:

$ printf "%(%-m/%-d/%Y)T\n" "Jan 24"
1/24/2014
$ LC_ALL=C printf "%(%b %-d)T\n" 1/24/2014
Jan 24

Portably

d=1/24/2014
echo "$d:Jan1:Feb2:Mar3:Apr4:May5:Jun6:Jul7:Aug8:Sep9:Oct10:Nov11:Dec12:" |
  sed -n 's|^0*\([0-9]\{1,2\}\)/0*\([0-9]\{1,2\}\)/.*:.*\(...\)\1:.*|\3 \2|p'

d='Jan 24'
echo "$d:Jan1Feb2Mar3Apr4May5Jun6Jul7Aug8Sep9Oct10Nov11Dec12" |
  sed -n "s|^\(...\) 0*\([0-9]\{1,\}\).*\1\([0-9]*\).*|\3/\2/$(date +%Y)|p"
0

FreeBSD and OS X date:

$ LC_ALL=C date -jf '%b %d' 'Jan 24' +%-d/%-m/%Y
24/1/2014
$ LC_ALL=C date -jf %d/%m/%Y 24/1/2014 '+%b %-d'
Jan 24

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.