Convert date formats to bash

I have a date in this format: "27 JUN 2011" and I want to convert it to 20110627

Is it possible to do in bash?

+48
date linux bash awk
Jun 28 2018-11-18T00:
source share
8 answers
#since this was yesterday date -dyesterday +%Y%m%d #more precise, and more recommended date -d'27 JUN 2011' +%Y%m%d #assuming this is similar to yesterdays 'date' question from you #http://stackoverflow.com/q/6497525/638649 date -d'last-monday' +%Y%m%d #going on @seth comment you could do this DATE="27 jun 2011"; date -d"$DATE" +%Y%m%d #or a method to read it from stdin read -p " Get date >> " DATE; printf " AS YYYYMMDD format >> %s" 'date -d"$DATE" +%Y%m%d' #which then outputs the following: #Get date >> 27 june 2011 #AS YYYYMMDD format >> 20110627 #if you really want to use awk echo "27 june 2011" | awk '{print "date -d\""$1FS$2FS$3"\" +%Y%m%d"}' | bash #note | bash just redirects awk output to the shell to be executed #FS is field separator, in this case you can use $0 to print the line #But this is useful if you have more than one date on a line 

More on the timing

note this only works on GNU date

I read this:

A version of Solaris that cannot support -d can be fixed by replacing the date version of sunfreeware.com

+71
Jun 28 2018-11-18T00:
source share
 date -d "25 JUN 2011" +%Y%m%d 

exits

 20110625 
+9
Jun 28 2018-11-18T00:
source share

Just with bash:

 convert_date () { local months=( JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC ) local i for (( i=0; i<11; i++ )); do [[ $2 = ${months[$i]} ]] && break done printf "%4d%02d%02d\n" $3 $(( i+1 )) $1 } 

And call it like this

 d=$( convert_date 27 JUN 2011 ) 

Or if the "old" date string is stored in a variable

 d_old="27 JUN 2011" d=$( convert_date $d_old ) # not quoted 
+6
Jun 28 '11 at 17:01
source share

On OSX, I use -f to specify the input format, -j, so as not to try to set the date and output format specifier. For example:

 $ date -j -f "%m/%d/%y %H:%M:%S %p" "8/22/15 8:15:00 am" +"%m%d%y" 082215 

Your example:

 $ date -j -f "%d %b %Y" "27 JUN 2011" +%Y%m%d 20110627 
+6
Aug 26 '15 at 14:57
source share

If you want to use the bash function, which works on both Mac OS X and Linux:

 # # Convert one date format to another # # Usage: convert_date_format <input_format> <date> <output_format> # # Example: convert_date_format '%b %d %T %Y %Z' 'Dec 10 17:30:05 2017 GMT' '%Y-%m-%d' convert_date_format() { local INPUT_FORMAT="$1" local INPUT_DATE="$2" local OUTPUT_FORMAT="$3" local UNAME=$(uname) if [[ "$UNAME" == "Darwin" ]]; then # Mac OS X date -j -f "$INPUT_FORMAT" "$INPUT_DATE" +"$OUTPUT_FORMAT" elif [[ "$UNAME" == "Linux" ]]; then # Linux date -d "$INPUT_DATE" +"$OUTPUT_FORMAT" else # Unsupported system echo "Unsupported system" fi } # Example: 'Dec 10 17:30:05 2017 GMT' => '2017-12-10' convert_date_format '%b %d %T %Y %Z' 'Dec 10 17:30:05 2017 GMT' '%Y-%m-%d' 
+3
Nov 22 '17 at 15:13
source share

It seems that one date command works well:

 date -d "27 JUN 2011" +%F 
+2
Jun 28 2018-11-18T00:
source share

Maybe something has changed since 2011, but it worked for me:

 $ date +"%Y%m%d" 20150330 

It is not necessary for -d get the same result.

+2
Mar 30 '15 at 22:36
source share

It is enough to do:

 data='date' datatime='date -d "${data}" '+%Y%m%d'' echo $datatime 20190206 

If you want to add also time you can use this way

 data='date' datatime='date -d "${data}" '+%Y%m%d %T'' echo $data Wed Feb 6 03:57:15 EST 2019 echo $datatime 20190206 03:57:15 
0
Feb 06 '19 at 8:59
source share



All Articles