If I have lets say this string "2008-12-12 19:21:10" how can I convert it into a date and get the year, month and day from that created object separately?
如果我有这个字符串"2008-12-12 19:21:10"我如何将它转换成一个日期,并从创建的对象中分别获得年、月和日?
4 个解决方案
#1
23
Use the datetime.datetime.strptime()
function:
使用datetime.datetime.strptime()函数:
from datetime import datetime
dt = datetime.strptime(datestring, '%Y-%m-%d %H:%M:%S')
Now you have a datetime.datetime
object, and it has .year
, .month
and .day
attributes:
现在有了datetime。datetime对象,它具有.year、.month和.day属性:
>>> from datetime import datetime
>>> datestring = "2008-12-12 19:21:10"
>>> dt = datetime.strptime(datestring, '%Y-%m-%d %H:%M:%S')
>>> print dt.year, dt.month, dt.day
2008 12 12
#2
0
https://www.tutorialspoint.com/python/time_strptime.htm Here you can find strptime() method complete description. where you can find all type of strings. Eg:- To convert string like this '15-MAY-12'
这里可以找到strptime()方法的完整描述。你可以找到所有类型的字符串。-将字符串转换成“15- 5- 12”
>>>from datetime import datetime
>>>datestring = "15-MAY-12"
>>>dt = datetime.strptime(datestring, '%d-%b-%Y')
>>>print(dt.year, dt.month, dt.day)enter code here
2012 MAY 15
#3
0
with milliseconds
以毫秒为单位
>>> from datetime import datetime
>>> datestring = "2018-04-11 23:36:18.886585"
>>> dt = datetime.strptime(datestring, '%Y-%m-%d %H:%M:%S.%f')
>>> print dt.year, dt.month, dt.day
2018 04 11
#4
0
One thing to add; pay attention that %y
is for two digit year notation %Y
is for the four digit one:
一件事添加;注意,%y表示两位数年表示法%y表示四位数1:
import datetime
datestring = '15-MAY-12'
print(datetime.datetime.strptime(datestring, '%d-%b-%y'))
>>> datetime.datetime(2012, 5, 15, 0, 0)
datestring = '15-MAY-2012'
print(datetime.datetime.strptime(datestring, '%d-%b-%Y'))
>>> datetime.datetime(2012, 5, 15, 0, 0)
#1
23
Use the datetime.datetime.strptime()
function:
使用datetime.datetime.strptime()函数:
from datetime import datetime
dt = datetime.strptime(datestring, '%Y-%m-%d %H:%M:%S')
Now you have a datetime.datetime
object, and it has .year
, .month
and .day
attributes:
现在有了datetime。datetime对象,它具有.year、.month和.day属性:
>>> from datetime import datetime
>>> datestring = "2008-12-12 19:21:10"
>>> dt = datetime.strptime(datestring, '%Y-%m-%d %H:%M:%S')
>>> print dt.year, dt.month, dt.day
2008 12 12
#2
0
https://www.tutorialspoint.com/python/time_strptime.htm Here you can find strptime() method complete description. where you can find all type of strings. Eg:- To convert string like this '15-MAY-12'
这里可以找到strptime()方法的完整描述。你可以找到所有类型的字符串。-将字符串转换成“15- 5- 12”
>>>from datetime import datetime
>>>datestring = "15-MAY-12"
>>>dt = datetime.strptime(datestring, '%d-%b-%Y')
>>>print(dt.year, dt.month, dt.day)enter code here
2012 MAY 15
#3
0
with milliseconds
以毫秒为单位
>>> from datetime import datetime
>>> datestring = "2018-04-11 23:36:18.886585"
>>> dt = datetime.strptime(datestring, '%Y-%m-%d %H:%M:%S.%f')
>>> print dt.year, dt.month, dt.day
2018 04 11
#4
0
One thing to add; pay attention that %y
is for two digit year notation %Y
is for the four digit one:
一件事添加;注意,%y表示两位数年表示法%y表示四位数1:
import datetime
datestring = '15-MAY-12'
print(datetime.datetime.strptime(datestring, '%d-%b-%y'))
>>> datetime.datetime(2012, 5, 15, 0, 0)
datestring = '15-MAY-2012'
print(datetime.datetime.strptime(datestring, '%d-%b-%Y'))
>>> datetime.datetime(2012, 5, 15, 0, 0)