首页 > 代码库 > datetime模块

datetime模块

datetime模块包括一些函数和类,用于完成日期和时间的解析、格式化和相关的运行

#!/usr/bin/env python
#coding: utf-8

import datetime

t = datetime.time(1,2,3)
print t #01:02:03
print t.hour    #1
print t.minute  #2
print t.second  #3
print t.microsecond #0
print t.tzinfo  #None
print datetime.time.min #00:00:00
print datetime.time.max #23:59:59.999999
print datetime.time.resolution  #0:00:00.000001 分辨率限制为整毫秒
today = datetime.date.today()
print today #2016-09-26
print today.ctime() #Mon Sep 26 00:00:00 2016
t1 = today.timetuple()
print t1.tm_year,t1.tm_mon,t1.tm_mday   #2016 9 26
print today.year,today.month,today.day  #2016 9 26
one_day = datetime.timedelta(days=1)
print one_day   #1 day, 0:00:00
yesterday = today - one_day
print yesterday #2016-09-25
tomorrow = today + one_day
print tomorrow  #2016-09-27
print tomorrow - yesterday  #2 days, 0:00:00
print yesterday - tomorrow  #-2 days, 0:00:00
print datetime.datetime.now()   #2016-09-26 21:40:49.278000
print datetime.datetime.today() #2016-09-26 21:40:49.278000
print datetime.datetime.utcnow()    #2016-09-26 13:40:49.278000
today = datetime.datetime.today()
format = "%a %b %d %H:%M:%S %Y"
print today #2016-09-26 21:44:27.670000
s = today.strftime(format)
print s     #Mon Sep 26 21:44:27 2016
d = datetime.datetime.strptime(s,format)
print d.strftime(format)    #Mon Sep 26 21:46:18 2016


本文出自 “天天向上goto” 博客,请务必保留此出处http://ttxsgoto.blog.51cto.com/4943095/1856769

datetime模块