Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to date in bash

I have a string in the format "yyyymmdd". It is a string in bash and I want to get it converted into a date so that all other date functions can be used on it.

"20121212" string into "20121212" date with format "%Y%m%d".

like image 234
Yahoo-Me Avatar asked Jun 21 '12 18:06

Yahoo-Me


People also ask

How do I convert a date to a string in bash?

Bash Date format YYYY-MM-DD To format date in YYYY-MM-DD format, use the command date +%F or printf "%(%F)T\n" $EPOCHSECONDS . The %F option is an alias for %Y-%m-%d . This format is the ISO 8601 format.

What is date +% s in bash?

date +%S. Displays seconds [00-59] date +%N. Displays in Nanoseconds.


1 Answers

This worked for me :

date -d '20121212 7 days' date -d '12-DEC-2012 7 days' date -d '2012-12-12 7 days' date -d '2012-12-12 4:10:10PM 7 days' date -d '2012-12-12 16:10:55 7 days' 

then you can format output adding parameter '+%Y%m%d'

like image 138
Nahuel Fouilleul Avatar answered Sep 21 '22 17:09

Nahuel Fouilleul