python中,怎么把字符串转换为日期格式
展开全部
python中要把字符串转换成日期格式需要使用time模块中的strptime函数,例子如下:
import time
t = time.strptime('2016-05-09 21:09:30', '%Y-%m-%d %H:%M:%S')
print(t)执行结果如下:
time.struct_time(tm_year=2016, tm_mon=5, tm_mday=9, tm_hour=21, tm_min=9, tm_sec=30, tm_wday=0, tm_yday=130, tm_isdst=-1)
函数说明:
第一个参数是要转换成日期格式的字符串,第二个参数是字符串的格式
函数官方文档如下:
Help on built-in function strptime in module time:
strptime(...)
strptime(string, format) -> struct_time
Parse a string to a time tuple according to a format specification.
See the library reference manual for formatting codes (same as
strftime()).
Commonly used format codes:
%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
Other codes may be available on your platform. See documentation for the C library strftime function.
import time
t = time.strptime('2016-05-09 21:09:30', '%Y-%m-%d %H:%M:%S')
print(t)执行结果如下:
time.struct_time(tm_year=2016, tm_mon=5, tm_mday=9, tm_hour=21, tm_min=9, tm_sec=30, tm_wday=0, tm_yday=130, tm_isdst=-1)
函数说明:
第一个参数是要转换成日期格式的字符串,第二个参数是字符串的格式
函数官方文档如下:
Help on built-in function strptime in module time:
strptime(...)
strptime(string, format) -> struct_time
Parse a string to a time tuple according to a format specification.
See the library reference manual for formatting codes (same as
strftime()).
Commonly used format codes:
%Y Year with century as a decimal number.
%m Month as a decimal number [01,12].
%d Day of the month as a decimal number [01,31].
%H Hour (24-hour clock) as a decimal number [00,23].
%M Minute as a decimal number [00,59].
%S Second as a decimal number [00,61].
%z Time zone offset from UTC.
%a Locale's abbreviated weekday name.
%A Locale's full weekday name.
%b Locale's abbreviated month name.
%B Locale's full month name.
%c Locale's appropriate date and time representation.
%I Hour (12-hour clock) as a decimal number [01,12].
%p Locale's equivalent of either AM or PM.
Other codes may be available on your platform. See documentation for the C library strftime function.
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
木兰花(池塘水绿风微暖)
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询