1

I am parsing a date as below:

date +'%Y-%m-%dT%H:%M:%SZ' -d '2017-10-06T13:41:06Z'

It works fine with date version (GNU coreutils) 8.21 but it is not working for date version (GNU coreutils) 8.4. Giving following error:

date: invalid date `2017-10-06T13:41:06Z'

Is there a way to parse this date in (GNU coreutils) 8.4 ?

4
  • 2
    Possible duplicate of Bash convert string to timestamp Commented Oct 6, 2017 at 8:39
  • @pacholik No, that thread is about parsing a different format. Commented Oct 6, 2017 at 8:49
  • @Gilles Different format makes it different question? Commented Oct 6, 2017 at 8:51
  • @pacholik Depends. In this case, the set of solutions is very different, so yes. There isn't a single way to parse dates, so there can't be a single question on parsing dates. Commented Oct 6, 2017 at 8:53

1 Answer 1

2

The feature you're using was added in GNU coreutils 8.13, so if you're using an older version, it isn't available.

Split the time at the T first. That works with older versions.

# Given an ISO date $iso_date with the time in split format HH:MM:SS...
date_part=${iso_date%%T*}
time_part=${iso_date#*T}
date -d "$date_part $time_part"

If you also need to support the condensed format with no punctuation, see a similar question on Unix Stack Exchange.

Sign up to request clarification or add additional context in comments.

1 Comment

Given that the shell is Bash, one can simply substitute T with a space: date -d "${iso_date/T/ }"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.