Python日期字符串到日期对象

时间:2010-05-10 15:22:12

标签: python date

如何在python中将字符串转换为日期对象?

字符串为:"24052010"(对应格式:"%d%m%Y"

我不想要datetime.datetime对象,而是datetime.date。

8 个答案:

答案 0 :(得分:506)

您可以在Python的strptime包中使用datetime

>>> datetime.datetime.strptime('24052010', "%d%m%Y").date()
datetime.date(2010, 5, 24)

答案 1 :(得分:75)

import datetime
datetime.datetime.strptime('24052010', '%d%m%Y').date()

答案 2 :(得分:46)

直接相关的问题:

如果你有

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

你得到:

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

你试过了:

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

但你仍然可以获得上面的追溯。

<强>答案:

>>> 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))

答案 3 :(得分:17)

如果你很懒,并且不想与字符串文字作斗争,你可以使用parser模块。

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

只需旁注,因为我们正在尝试匹配any字符串表示,它比strptime慢10倍

答案 4 :(得分:3)

还有另一个名为arrow的库非常适合在python日期进行操作。

import arrow
import datetime

a = arrow.get('24052010', 'DMYYYY').date()
print(isinstance(a, datetime.date)) # True

答案 5 :(得分:2)

你有这样的日期字符串,&#34; 24052010&#34;你需要日期对象,

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

这个cus_date会给你一个日期对象。

您可以使用此

从日期对象中检索日期字符串
cus_date.strftime("%d%m%Y")

答案 6 :(得分:2)

对于单个值,datetime.strptime方法是最快的

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)

对于值列表,熊猫pd.to_datetime是最快的

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)

对于ISO8601日期时间格式,ciso8601是火箭

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)

答案 7 :(得分:1)

使用时间模块转换数据。

代码段:

import time 
tring='20150103040500'
var = int(time.mktime(time.strptime(tring, '%Y%m%d%H%M%S')))
print var
相关问题