483

How do I convert a string to a date object in python?

The string would be: "24052010" (corresponding to the format: "%d%m%Y")

I don't want a datetime.datetime object, but rather a datetime.date.

Asclepius
  • 57,944
  • 17
  • 167
  • 143
elif
  • 5,427
  • 3
  • 28
  • 28

9 Answers9

772

You can use strptime in the datetime package of Python:

>>> import datetime
>>> datetime.datetime.strptime('24052010', "%d%m%Y").date()
datetime.date(2010, 5, 24)
Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
SilentGhost
  • 307,395
  • 66
  • 306
  • 293
  • I am trying a similar process except for my input string do not have the year, so it looks like '2405'. By default, the year is taken as `1900`. The issue occurs when parsing Feb date like '2902'. I get this error `ValueError: day is out of range for month`. Not sure how I can set the default year while parsing. – Shubham Naik Apr 19 '20 at 13:28
104
import datetime
datetime.datetime.strptime('24052010', '%d%m%Y').date()
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
84

Directly related question:

What if you have

datetime.datetime.strptime("2015-02-24T13:00:00-08:00", "%Y-%B-%dT%H:%M:%S-%H:%M").date()

and you get:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/_strptime.py", line 308, in _strptime
    format_regex = _TimeRE_cache.compile(format)
  File "/usr/local/lib/python2.7/_strptime.py", line 265, in compile
    return re_compile(self.pattern(format), IGNORECASE)
  File "/usr/local/lib/python2.7/re.py", line 194, in compile
    return _compile(pattern, flags)
  File "/usr/local/lib/python2.7/re.py", line 251, in _compile
    raise error, v # invalid expression
sre_constants.error: redefinition of group name 'H' as group 7; was group 4

and you tried:

<-24T13:00:00-08:00", "%Y-%B-%dT%HH:%MM:%SS-%HH:%MM").date()

but you still get the traceback above.

Answer:

>>> from dateutil.parser import parse
>>> from datetime import datetime
>>> parse("2015-02-24T13:00:00-08:00")
datetime.datetime(2015, 2, 24, 13, 0, tzinfo=tzoffset(None, -28800))
Jean-François Corbett
  • 37,420
  • 30
  • 139
  • 188
Schopenhauer
  • 1,132
  • 8
  • 8
  • 2
    2015-07-20 09:46:55+00:00 i have this kind of data, How do i get date object ? – Hardik Gajjar Feb 09 '16 at 08:16
  • Use the re (regular expression) module to change your data from "2015-07-20 09:46:55+00:00" to "2015-07-20T09:46:55-00:00". Then use dateutil.parse to obtain a date object. – Schopenhauer Feb 09 '16 at 08:22
  • 1
    Thank you , actually problem was dict type object and i just get solution using dict.get using – Hardik Gajjar Feb 09 '16 at 08:51
  • 2
    Another idea is to read the manual https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior and notice that %B stands for "Month as locale’s full name." which is like "January" or "December" so "02" is not going to parse. – Flip Feb 23 '18 at 14:44
  • 2
    The date format string in this is incorrect, it should be a %m not a %B – theannouncer Nov 30 '18 at 05:10
  • You can fix your example by using: `datetime.strptime("2015-02-24T13:00:00+08:00", "%Y-%m-%dT%H:%M:%S%z")` Additionally if your time uses milliseconds: `datetime.strptime("2022-04-07T08:53:42.06717+02:00", "%Y-%m-%dT%H:%M:%S.%f%z")` – NicoHood Apr 07 '22 at 07:35
51

If you are lazy and don't want to fight with string literals, you can just go with the parser module.

from dateutil import parser
dt = parser.parse("Jun 1 2005  1:33PM")
print(dt.year, dt.month, dt.day,dt.hour, dt.minute, dt.second)
>2005 6 1 13 33 0

Just a side note, as we are trying to match any string representation, it is 10x slower than strptime

user1767754
  • 23,311
  • 18
  • 141
  • 164
  • 1
    very simply solution, thanks. I was trying to convert an Excel long date that was being displayed as `Monday, June 03, 2019` – Jeff Bluemel Jun 05 '19 at 13:33
11

you have a date string like this, "24052010" and you want date object for this,

from datetime import datetime
cus_date = datetime.strptime("24052010", "%d%m%Y").date()

this cus_date will give you date object.

you can retrieve date string from your date object using this,

cus_date.strftime("%d%m%Y")
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
7

For single value the datetime.strptime method is the fastest

import arrow
from datetime import datetime
import pandas as pd

l = ['24052010']

%timeit _ = list(map(lambda x: datetime.strptime(x, '%d%m%Y').date(), l))
6.86 µs ± 56.5 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

%timeit _ = list(map(lambda x: x.date(), pd.to_datetime(l, format='%d%m%Y')))
305 µs ± 6.32 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%timeit _ = list(map(lambda x: arrow.get(x, 'DMYYYY').date(), l))
46 µs ± 978 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

For a list of values the pandas pd.to_datetime is the fastest

l = ['24052010'] * 1000

%timeit _ = list(map(lambda x: datetime.strptime(x, '%d%m%Y').date(), l))
6.32 ms ± 89.6 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

%timeit _ = list(map(lambda x: x.date(), pd.to_datetime(l, format='%d%m%Y')))
1.76 ms ± 27.3 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

%timeit _ = list(map(lambda x: arrow.get(x, 'DMYYYY').date(), l))
44.5 ms ± 522 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

For ISO8601 datetime format the ciso8601 is a rocket

import ciso8601

l = ['2010-05-24'] * 1000

%timeit _ = list(map(lambda x: ciso8601.parse_datetime(x).date(), l))
241 µs ± 3.24 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Grzegorz
  • 1,268
  • 11
  • 11
4

There is another library called arrow really great to make manipulation on python date.

import arrow
import datetime

a = arrow.get('24052010', 'DMYYYY').date()
print(isinstance(a, datetime.date)) # True
Kruupös
  • 5,097
  • 3
  • 27
  • 43
2

string "24052010" In a very manual way you could just go like this:- first split the string as (yyyy-mm-dd) format so you could get a tuple something like this (2010, 5, 24), then simply convert this tuple to a date format something like 2010-05-24.

you could run this code on a list of string object similar to above and convert the entire list of tuples object to date object by simply unpacking(*tuple) check the code below.

import datetime
#for single string simply use:-

my_str= "24052010"
date_tup = (int(my_str[4:]),int(my_str[2:4]),int(my_str[:2]))
print(datetime.datetime(*date_tup))

output: 2012-01-01 00:00:00 

# for a list of string date objects you could use below code.
date_list = []
str_date = ["24052010", "25082011", "25122011","01012012"]

for items in str_date:
    date_list.append((int(items[4:]),int(items[2:4]),int(items[:2])))

for dates in date_list:
    # unpack all tuple objects and convert to date
    print(datetime.datetime(*dates))

output:
2010-05-24 00:00:00
2011-08-25 00:00:00
2011-12-25 00:00:00
2012-01-01 00:00:00 
Manendar
  • 19
  • 3
1

Use time module to convert data.

Code snippet:

import time 
tring='20150103040500'
var = int(time.mktime(time.strptime(tring, '%Y%m%d%H%M%S')))
print var
Shreyash S Sarnayak
  • 2,309
  • 19
  • 23
shashankS
  • 1,043
  • 1
  • 11
  • 21