本文目录一览:
- python中,怎么把字符串转换为日期格式
- python中,怎么把字符串转换为日期格式
- python中如何把datetime.datetime转换成datetime.time?
- [新手求教:python 时间格式转换](#新手求教:python 时间格式转换)
- python的日期类型转换
python中,怎么把字符串转换为日期格式
- python中要把字符串转换成日期格式需要使用time模块中的strptime函数,例子如下:
执行结果如下:
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)
- 函数说明: 第一个参数是要转换成日期格式的字符串,第二个参数是字符串的格式,下面函数可以看一下。
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.
python中如何把datetime.datetime转换成datetime.time?
用Python实现字符串和日期相互转换的方法,具体如下:这里用的分别是time和datetime函数来处理
import time, datetime
# datetostr
# 输出时间
print(time.strftime("%Y-%m-%d%X", time.localtime()))
# strtodate
# 字符串转化为日期
t = time.strptime("2016-12-05", "%Y-%m-%d")
y, m, d = t[0:3]
# 输出时间
print(datetime.datetime(y, m, d))
新手求教:python 时间格式转换
时间格式转换分为两种,时间转换为字符串和字符串转换为时间,具体代码例子如下:
import datetime
import time
# 日期转换为字符串,使用strftime()函数
# time.strftime(format[, t])
print(datetime.datetime.now())
print(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
print(datetime.datetime.now().strftime("%b %d %Y %H:%M:%S"))
print(datetime.datetime.now().strftime("%c %d %Y %H:%M:%S"))
# 字符串转换为日期,使用strptime()函数
t = (2009, 2, 17, 8, 3, 38, 1, 48, 0)
t = time.mktime(t)
print(time.strftime("%b %d %Y %H:%M:%S", time.gmtime(t)))
print(time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(t)))
注:格式字符说明:
python中时间日期格式化符号:
%y
两位数的年份表示(00-99)%Y
四位数的年份表示(000-9999)%m
月份(01-12)%d
月内中的一天(0-31)%H
24小时制小时数(0-23)%I
12小时制小时数(01-12)%M
分钟数(00=59)%S
秒(00-59)%a
本地简化星期名称%A
本地完整星期名称%b
本地简化的月份名称%B
本地完整的月份名称%c
本地相应的日期表示和时间表示%j
年内的一天(001-366)%p
本地A.M.或P.M.的等价符%U
一年中的星期数(00-53)星期天为星期的开始%w
星期(0-6),星期天为星期的开始%W
一年中的星期数(00-53)星期一为星期的开始%x
本地相应的日期表示%X
本地相应的时间表示%Z
当前时区的名称%%
%号本身
python的日期类型转换
你可以利用 time 模块里的 strptime() 和 strftime()。
strptime()
根据你指定的格式控制字符串解读日期strftime()
则根据你指定的格式控制字符串输出日期 比如,把 "12-Jan-06 10:06" 格式转换成 "2006-01-12 10:06:00" 格式:
from time import strptime, strftime
myDate = '12-Jan-06 10:06'
parsed = strptime(myDate, '%d-%b-%y %H:%M')
converted = strftime('%Y-%m-%d %H:%M:00', parsed)
converted
结果:
'2006-01-12 10:06:00'