1

I have a string that says "Oct. 2, 2017, midnight" and want to convert it to a UTC timestamp.

Is there any good way to do this?

6
  • are all the hours midnight? Commented Jan 23, 2018 at 19:37
  • Yeah all the hours are midnight. I actually did a little searching and found it seems to just be a normal date object with the format: 2017-10-02 00:00:00+00:00 Commented Jan 23, 2018 at 19:39
  • Did you copy/paste your post title into Google and go through all the solutions? Commented Jan 23, 2018 at 19:40
  • 1
    Possible duplicate of Convert string date to timestamp in Python Commented Jan 23, 2018 at 19:43
  • 1
    @ScottMermelstein I thought the same but the "midnight" string is kind of an interesting variant on the common problem. Commented Jan 23, 2018 at 19:44

1 Answer 1

2

Considering all your data is at midnight, the easiest way is to remove the , midnight string and convert it.

import datetime
import pytz
s = "Oct. 2, 2017, midnight"
dt = datetime.datetime.strptime(s.split(', midnight')[0], '%b. %d, %Y')
dt_utc = dt.astimezone(pytz.utc)

If there are other time indicated, you'll want to interpret the literals of the time and pass it onto strptime() accordingly.

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.