0

I want to convert it from

Format 1: 12272019(this is just a number)

To

Format 2: 2019-12-27

I tried using below: date -d '12272019' +%Y-%m-%d

But it is showing invalid date format

1
  • 1
    It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the poster has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if there is any), the expected output, and the output you actually get (console output, tracebacks, etc.). The more detail you provide, the more answers you are likely to receive. Check the FAQ and How to Ask. Commented Jan 6, 2020 at 8:26

2 Answers 2

3

The date command only accepts a predefined set of formats for its input. Your format mmddyyyy is not part of that set.

You can re-arrange your date string manually using either sed:

sed -E 's/(..)(..)(....)/\3-\1-\2/' <<< 12272019 

or bash:

date=12272019 echo "${date:4}-${date:0:2}-${date:2:2}" 
Sign up to request clarification or add additional context in comments.

Comments

0

On OS/X, date is quite a bit different, so you can specify the input format for date:

date -jf "%m%d%Y" 12272019 +"%Y-%m-%d" # => 2019-12-27 

Linux does not allow you to do that though, but you can do it easily with sed:

echo 12272019 | sed -e 's/\(..\)\(..\)\(....\)/\3-\1-\2/' # => 2019-12-27 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.