Convert date formats in bash

Convert date formats in bash

Asked on December 28, 2018 in Bash.
Add Comment


  • 12 Answer(s)

    Use this code,

    #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's 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's 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
    

    For more information about date.

    The code above works on only for GNU date.

    sunfreeware.com version of date is support for -d, not Solaris version of date

    Answered on December 28, 2018.
    Add Comment

    Try this one line code,

    date -d "25 JUN 2011" +%Y%m%d
    

    Yields,

    20110625
    
    Answered on December 28, 2018.
    Add Comment

    Use the following code for convert the date format in bash.

    #
    # 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'
    
    Answered on December 28, 2018.
    Add Comment
    #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's 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's 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
    Answered on February 24, 2019.
    Add Comment
    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
    }
    Answered on February 28, 2019.
    Add Comment

    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 invoke 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
    Answered on February 28, 2019.
    Add Comment
    #
    # 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'
    Answered on February 28, 2019.
    Add Comment
    data=`date`
    datatime=`date -d "${data}" '+%Y%m%d'`
    echo $datatime
    20190206
    Answered on February 28, 2019.
    Add Comment
    data=`date`
    datatime=`date -d "${data}" '+%Y%m%d %T'`
    
    echo $data
    Wed Feb 6 03:57:15 EST 2019
    
    Answered on February 28, 2019.
    Add Comment

    Linux Syntax To Format Date For Display On Screen

    The syntax is as follows for the GNU/date and BSD/date command:
    date +FORMAT
    date +"%FORMAT"
    date +"%FORMAT%FORMAT"
    date +"%FORMAT-%FORMAT"

    An operand with a leading plus (+) sign signals a user-defined format string which specifies the format in which to display the date and time. The following examples are tested on GNU/Linux, Apple OS X Unix, and FreeBSD unix operating system

    Answered on February 28, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.