2

I am passing date as variable in function. I want to convert that date in different format. Example

DT=2015-12-08

Want to convert DT to 08-dec-2015 in Unix.

  • Using what programming language? – Majenko Dec 13 '15 at 18:07
2

Assuming GNU date (i.e. non-embedded Linux):

$ LC_TIME=C date --date=2015-12-08 +%d-%b-%Y
08-Dec-2015
| improve this answer | |
1

If you're on a non-embedded Linux, or more generally a system with GNU date, you can use it to typeset an arbitrary date.

LC_ALL=C date -d "$DT" +%d-%b-%Y | tr A-Z a-z

If you don't have GNU date (or even if you do), you can do this in pure shell.

case $DT in
  *-01-*|*-1-*) month=jan;;
  *-02-*|*-2-*) month=feb;;
  *-03-*|*-3-*) month=mar;;
  *-04-*|*-4-*) month=apr;;
  *-05-*|*-5-*) month=may;;
  *-06-*|*-6-*) month=jun;;
  *-07-*|*-7-*) month=jul;;
  *-08-*|*-8-*) month=aug;;
  *-09-*|*-9-*) month=sep;;
  *-10-*) month=oct;;
  *-11-*) month=nov;;
  *-12-*) month=dec;;
esac
echo "${DT##*-}-$month-${DT%%-*}"
| improve this answer | |

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.