您的位置:首页 > 编程语言 > Python开发

python time,datetime,string转换

2013-02-20 13:44 344 查看
#把datetime转成字符串  

def datetime_toString(dt):  

    return dt.strftime("%Y-%m-%d %H:%M:%S")  

  

#把字符串转成datetime  

def string_toDatetime(string):  

    return datetime.strptime(string, "%Y-%m-%d %H:%M:%S")  

  

#把字符串转成时间戳形式  

def string_toTimestamp(strTime):  

    return time.mktime(time.strptime(strTime,'%Y-%m-%d %H:%M:%S'))

#把时间戳转成字符串形式  

def timestamp_toString(stamp):  

    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(stamp))  

  

#把datetime类型转外时间戳形式  

def datetime_toTimestamp(dateTime):  

    return time.mktime(dateTime.timetuple())  

 

#时间戳转datetime形式

datetime.datetime.fromtimestamp(1345270963)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: