Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create datetime object from "16SEP2012" in python

I can create datetime objects in python this way:

import datetime  new_date= datetime.datetime(2012,09,16) 

How can I create same datetime object from a string in this format: "16SEP2012" ?

like image 206
alwbtc Avatar asked Sep 08 '13 13:09

alwbtc


People also ask

How do you create a datetime object in Python?

To create a date, we can use the datetime() class (constructor) of the datetime module. The datetime() class requires three parameters to create a date: year, month, day.

How do you convert an object to a date in Python?

The date column is indeed a string, which—remember—is denoted as an object type in Python. You can convert it to the datetime type with the . to_datetime() method in pandas .

How do I make datetime timezone aware?

Timezone aware object using datetime now(). time() function of datetime module. Then we will replace the value of the timezone in the tzinfo class of the object using the replace() function. After that convert the date value into ISO 8601 format using the isoformat() method.


1 Answers

Use datetime.datetime.strptime:

>>> datetime.datetime.strptime('16Sep2012', '%d%b%Y') datetime.datetime(2012, 9, 16, 0, 0) 
like image 68
falsetru Avatar answered Oct 04 '22 00:10

falsetru