1

I've got a cURL command that gets a date and sets it to a variable as a string. But then I want to use that variable as a date so I use the date command on the variable but it seems like the date command doesn't like this. How do I go about setting this variable as a date?

Example:

date=$(curl blahblahblah)
echo $date
"2017-11-02T13:23:52+00:00"
date -d $date +%s
date: invalid date ‘"2017-11-02T13:23:52+00:00"’

When I substitute $date for the actual value, it works fine.

1 Answer 1

1

Output from curl has double quotes and your date variable has those quotes around the date value.

In bash you can do this to strip double quotes before using date command:

date -d "${date//\"/}" '+%s'
1509629032

If you don't have bash then use:

date -d $(echo "$date" | tr -d '\"') '+%s'
1509629032
Sign up to request clarification or add additional context in comments.

Comments

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.