python 时间处理

import time,datetime
# date to str
print time.strftime( "%Y-%m-%d %X" , time.localtime())
#str to date
t = time.strptime( "2009 - 08 - 08" , "%Y - %m - %d" )
print(time.mktime(t)) #转换为秒值
y,m,d = t[ 0 : 3 ]
print datetime.datetime(y,m,d)



2015 - 05 - 13 10 : 43 : 07
2009 - 08 - 08 00 : 00 : 00


#!/usr/bin/env python
# -*- coding: cp936 -*-


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.datetime.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

运行结果:

1377507480.0

2013-08-26 16:58:00





你可能感兴趣的:(python 时间处理)