本文摘自php中文网,作者黄舟,侵删。
时间上下限
1 2 3 | import datetime
print 'min : ' , datetime.datetime. min
print 'max : ' , datetime.datetime. max
|
1 2 | min : 0001 - 01 - 01 00 : 00 : 00
max : 9999 - 12 - 31 23 : 59 : 59.999999
|
datetime.datetime.now() 模块
1 2 3 4 5 6 7 | import datetime
print '.now() : ' , datetime.datetime.now()
print type (datetime.datetime.now())
print '.now().date(): ' , datetime.datetime.now().date()
print type (datetime.datetime.now().date())
print '.strftime : ' , datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S" )
print type (datetime.datetime.now().strftime( "%Y-%m-%d %H:%M:%S" ))
|
1 2 3 | .now() : 2017 - 09 - 06 19 : 46 : 23.099767 < type 'datetime.datetime' >
.now().date(): 2017 - 09 - 06 < type 'datetime.date' >
.strftime : 2017 - 09 - 06 19 : 46 : 23 < type 'str' >
|
获取单个时间信息
1 2 3 4 5 6 7 | import datetime
print 'year : ' , datetime.datetime.now().year
print 'month : ' , datetime.datetime.now().month
print 'day : ' , datetime.datetime.now().day
print 'hour : ' , datetime.datetime.now().hour
print 'minute : ' , datetime.datetime.now().minute
print 'second : ' , datetime.datetime.now().second
|
1 2 3 4 5 6 | year : 2017
month : 9
day : 6
hour : 19
minute : 47
second : 13
|
datetime 其他模块
1 2 3 | import datetime
print '.time() : ' , datetime.time()
print '.date.today(): ' , datetime.date.today()
|
1 2 | .time() : 00 : 00 : 00
.date.today(): 2017 - 09 - 06
|
计算非今日的时间信息
1 2 3 4 | import datetime
print 'tomorrow: ' , datetime.date.today() + datetime.timedelta(days = 1 )
print 'tomorrow: ' , datetime.datetime.now() + datetime.timedelta(days = 1 )
print 'tomorrow: ' , (datetime.datetime.now() + datetime.timedelta(days = 1 )).strftime( "%Y-%m-%d %H:%M:%S" )
|
1 2 3 | tomorrow: 2017 - 09 - 07
tomorrow: 2017 - 09 - 07 19 : 49 : 16.292580
tomorrow: 2017 - 09 - 07 19 : 49 : 16
|
time 模块 显示时间
1 2 | import timeprint 'time.time() : ' , time.time()
print 'time.localtime(): ' , time.localtime()
|
1 2 | time.time() : 1504698623.85
time.localtime(): time.struct_time(tm_year = 2017 , tm_mon = 9 , tm_mday = 6 , tm_hour = 19 , tm_min = 50 , tm_sec = 23 , tm_wday = 2 , tm_yday = 249 , tm_isdst = 0 )
|
推迟线程调用
以上就是python中time模块与datetime模块的详解的详细内容,更多文章请关注木庄网络博客!!
相关阅读 >>
如何在网上自学Python?
Python 如何自定义模块?
如何列出一个目录的所有文件
Python 循环语句之 while,for语句详解
Python利用smtplib实现qq邮箱发送邮件
Python如何逆序输出数组
Python教你高效办公,自制屏幕翻译工具
Python通过什么划分语句块
Python开发什么
爬虫&问题解决&思考
更多相关阅读请进入《Python》频道 >>
人民邮电出版社
python入门书籍,非常畅销,超高好评,python官方公认好书。
转载请注明出处:木庄网络博客 » python中time模块与datetime模块的详解