0

A variety of programs output date formats according to the syntax of their Unix platforms date command. For example: Tue Nov 5 12:38:00 EST 2013.

How can I easily convert this into a Python date object?

1 Answer 1

1

The answer is actually pretty simple. You just need to use the datetime.strptime() method which converts a string representation of a date (1st parameter) into a date object based on a directive which specifies that format of the string representation (2nd parameter).

In this case, this is the code you would use:

import datetime

unix_date_format = '%a %b %d %H:%M:%S %Z %Y'
# Matches strings like Tue Nov 5 12:38:00 EST 2013

my_date = datetime.datetime.strptime(
    date_in_string_format, unix_date_format)

Further Reading
datetime.strptime() method

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.