1

How can i format the date "01-SEP-2011" on "2011-09-01" format in shell script?

3 Answers 3

6

Try this:

$ date --date='01-SEP-2011' +%Y-%m-%d
Sign up to request clarification or add additional context in comments.

2 Comments

That assumes you have the GNU coreutils version of the date command. If you're running bash, you probably do -- but it might not be installed by default on MacOS, for example. (I don't know whether bash is installed by default, but there's no non-GNU version of bash; there are non-GNU versions of date.)
date ... +%F also works (but I think +%Y-%m-%d is a bit more portable).
1

If you have Ruby installed on the target system you could do this:

#/bin/bash
read DATE # 01-SEP-2011
set DATE=$(ruby -rdate -e "puts Date.parse('$DATE').to_s")
echo $DATE # 2011-09-01

Comments

0
kent$  date +%F -d "01-SEP-2011" 
2011-09-01

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.