python把日期转换为秒数;日期转为字符串;datetime、date

时间:2024-08-06 21:36:38

1、秒数是相对于1970.1.1号的秒数

2、日期的模块有time、datetime

3、

import datetime
t = datetime.datetime(2009, 10, 21, 0, 0, 10, 10) 分别是年份、月份、日、小时、分钟、秒、微妙(10-6秒)
print (t-datetime.datetime(1970,1,1)).total_seconds() 总共多少秒
>>> import datetime, time
>>> t = datetime.datetime(2011, 10, 21, 0, 0)
>>> time.mktime(t.timetuple())
1319148000.0

#string转datetime

>>>str = '2012-11-19'

>>>date_time = datetime.datetime.strptime(str,'%Y-%m-%d')

>>>date_time

datetime.datetime(2012,11,19,0,0)

#datetime转string

>>>date_time.strftime('%Y-%m-%d')

'2012-11-19'

#datetime转时间戳

>>>time_time = time.mktime(date_time.timetuple())

>>>time_time

1353254400.0

#时间戳转string

>>>time.strftime('%Y-%m-%d',time.localtime(time_time))

'2012-11-19'

#date转datetime

>>>date = datetime.date.today()

>>>date

>>>datetime.date(2012,11,19)

>>>datetime.datetime.strptime(str(date),'%Y-%m-%d')    #将date转换为str,在由str转换为datetime

>>>datetime.datetime(2012,11,19,0,0)

参考:

1、https://*.com/questions/7852855/how-to-convert-a-python-datetime-object-to-seconds

2、https://www.jianshu.com/p/03d6e9867fdf