Search code examples
bashdateparsingunixdata-conversion

How do I convert a date string in format MMDDYYYY to YYYY-MM-DD at the Unix command line?


From the Unix command line, how do I convert a date string in the format MMDDYYYY to the format YYYY-MM-DD? For instance, how do I convert 02032017 to 2017-02-03? Thanks!

I have tried this, but I'm afraid it did not work.

date -d "02032017"

This produces this error message.

date: invalid date ‘02032017’

Solution

  • String manipulation with variable expansions

    input_date=02032017
    output_date=${input_date:4}-${input_date::2}-${input_date:2:2}