Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert YYYYMMDDHHMMSS to a date readable by `date`

Tags:

date

linux

bash

sed

I have a set of date/time strings in the YYYYMMDDHHMMSS format that I want to convert to something readable by the date utility. Usually, I can do something like:

date -d "2010-10-01 12:34:56"

However, date does not like the YYYYMMDDHHMMSS:

date -d "20100101123456"..invalid date

So, I probably need to refine the string to be in the prior format. I'm thinking sed is the answer, but it gets ugly very fast. I'm quite certain my strings will be the proper format, so how do I easily convert them?

like image 778
User1 Avatar asked Oct 31 '25 01:10

User1


1 Answers

date doesn't allow "YYYYMMDDHHMMSS", but it does "YYYYMMDD HH:MM:SS", so:

D="20100101123456"
date -d "${D:0:8} ${D:8:2}:${D:10:2}:${D:12:2}"
like image 99
chris Avatar answered Nov 01 '25 17:11

chris



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!