hcwzwx 发表于 2015-12-15 12:48:23

python时间转换

转载:http://www.xinghaixu.com/archives/685
import time
import datetime


def ISOString2Time(s):
    '''
    convert a ISO format time to second
    from:2006-04-12 16:46:40 to:23123123
    把一个时间转化为秒
    '''
    d = datetime.datedatetime.strptime(s, "%Y-%m-%d %H:%M:%S")
    return time.mktime(d.timetuple())


def Time2ISOString(s):
    '''
    convert second to a ISO format time
    from: 23123123 to: 2006-04-12 16:46:40
    把给定的秒转化为定义的格式
    '''
    return time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(float(s)))

if __name__ == '__main__':
    a = "2013-08-26 16:58:00"
    b = ISOString2Time(a)
    print b
    c = Time2ISOString(b)
    print c
页: [1]
查看完整版本: python时间转换