I'm wondering if is possible to convert a date as show Oct 31 00:00:00 2013 GMT
to 10-31-2013
.
I'm getting the date as follow:
NotBeforeDate=$(openssl x509 -noout -in ${CERTIFICATE} -dates | grep "notBefore")
The date that I'm getting is Oct 31 00:00:00 2013 GMT
and I wanted to convert it to 10-31-2013
.
There's any command that could do that? Or do I have to do it all manually?
If so, the best way is create my own function and send the long date as parameter and return a short date.
The openssl command will make the NotBeforeDate
variable to have the value (at least in the bash version I'm using):
notBefore=Oct 31 00:00:00 2013 GMT
So, first we need to remove the notBefore=
part:
dateStr=${NotBeforeDate/notBefore=/}
Then you can use the date
command:
date --date="$dateStr" --utc +"%m-%d-%Y"
The --date
option tells the command to use the dateStr
value, --utc
tells that the date is in UTC (as specified by GMT
part) and +"%m-%d-%Y"
formats the date to the desired format.
The output is:
10-31-2013
PS: the options can vary according to your Linux version.
You can check all the available ones with date --help
or man date
.
For example, the long options --date
and --utc
might not be available, but the equivalent short versions might be (just an example, I'm not sure if date
command has such variations between different unix versions):
date -d "$dateStr" -u +"%m-%d-%Y"
Unfortunately I don't have the exact same environment you're using (ksh in unix), but that should work.
The -d
options seems to be GNU specific, so if it's not available, you'll have to manually parse the string. Assuming that dateStr
has the value Oct 31 00:00:00 2013 GMT
, you can run:
printf '%s\n' "$dateStr" | awk '{ printf "%02d-%02d-%04d\n", (index("JanFebMarAprMayJunJulAugSepOctNovDec",$1)+2)/3, $2, $4}'
The output is:
10-31-2013