PDA

View Full Version : [SOLVED] Converting a string to a date in bash using date utility



trilobite2
July 5th, 2018, 08:35 AM
Hi all -

I'm trying to convert a string to a date in bash using the date utility but it isn't working. Unfortunately, the string is in double-quotes and the date uses slashes as the separators.

I'm doing this (where $ is the prompt) -


$ date -d "28/06/2018" +'%d/%m/%Y'


This gives me an error - "date: invalid date ‘28/06/2018’ "

I hope someone can help! Thanks very much -
- trilobite2

The Cog
July 5th, 2018, 08:50 AM
Although date can output a date in any format you like, "man date" says expects any input date to be in the format "[MMDDhhmm[[CC]YY][.ss]]" ., although it seems to accept MM/DD/YYYY which is that strange American way to write dates:

~$ date -d "06/28/2018" +'%d/%m/%Y'
28/06/2018

trilobite2
July 5th, 2018, 10:32 AM
Hi, The Cog - thanks for that!

Ok - that shouldn't be a problem for me. I can just chop the date into three parts and reassemble it in the desired format.

I'm happy - I'll mark this as solved!

Cheers - thanks again.... :)
- trilobite2