0

How can I convert 14/Jul/2015 into 14-08-2015?

It is saying invalid date format because of the Jul element, but if I put 07 then it works fine.

I have tried this:

echo "Please enter the date: "
read X (here X is 14/Jul/2015)
a= date --date=$X '+%d-%b-%y'
echo "$a"

2 Answers2

1

If $x contains 14/Jul/2015, then use this:

date -d "${x//\//-}" '+%d-%m-%Y'

It will print:

14-07-2015

The date utillity doesn't understand the slash separated string, so you have to remove it and replace it with a dash (-).

chaos
  • 48,171
0

Your problem is not with Jul, but with /, so remove them using for example parameter substitution mechanism:

echo "Please enter the date: "
read X
a=$(date --date="${X//\//-}" '+%d-%b-%y')
echo "$a"

Please note that your variable assignment was also wrong (there must not be space after =) and command substitution should be enclosed with $().

jimmij
  • 47,140