首页 > 编程语言 > 详细

python中strftime和strptime函数

时间:2019-08-29 14:56:32      阅读:79      评论:0      收藏:0      [点我收藏+]

strftime和strptime函数均来自包datetime

from datetime import *

strftime:

将datetime包中的datetime类,按照入参格式生成字符串变量

from datetime import *
currenttime=datetime.now()  #生成当前时间的datetime类实例
print(type of currenttime, type(currenttime))
print(currenttime)
cur=currenttime.strftime(%Y_%m_%d-%H-%M-%S)
print(type of cur, type(cur))
print(cur)

输出

type of currenttime <class datetime.datetime>
2019-08-29 14:01:39.973547
type of cur <class str>
2019_08_29-14-01-39

strptime:

将字符串根据其格式,提取所含时间,并生成datetime类实例

from datetime import *
strdate=2019:08:29:09-00-00
strdatetime=datetime.strptime(strdate,%Y:%m:%d:%H-%M-%S)
print(type of strdatetime:,type(strdatetime),:,strdatetime)

输出

type of strdatetime: <class datetime.datetime> : 2019-08-29 09:00:00

注意:strptime中的第二个参数是输入字符串的格式,而不是出参格式,datetime实例的格式是固定的

 

python中strftime和strptime函数

原文:https://www.cnblogs.com/mghhzAnne/p/11429447.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!