python中 timedelta对象学习记录

上一篇 / 下一篇  2017-04-23 10:30:12 / 个人分类:python

timedelta介绍:具体各个参数介绍及使用方法见下面代码,标红部分为注释及输出
对象初始化输入数据如下:
datetime.
timedelta([days[,seconds[,microseconds[,milliseconds[,minutes[,hours[,weeks]]]]]]])

#coding:utf-8
import os
import datetime
from os import path
if __name__ == '__main__':   
   date =  datetime.timedelta.max#最大值为999999999 days, 23:59:59.999999
   print date
   print date.days,date.seconds,date.microseconds#999999999 86399 999999
  
   print datetime.timedelta.min#最小值为-999999999 days, 0:00:00
   print datetime.timedelta()#默认值为全0 0:00:00
  #days的取值范围为-999999999 <= days <= 999999999
   print datetime.timedelta(days=-999999999)#
   print datetime.timedelta(days=-999999999)#
  #days的值超出范围后就会提示OverflowError: days=-1000000000; must have magnitude <= 999999999
   #print datetime.timedelta(days=-1000000000)
   #OverflowError: days=1000000000; must have magnitude <= 999999999
   # print datetime.timedelta(days=1000000000)
  
  #seconds 的取值范围为0 <= seconds < 3600*24超出后会进行跳变
   print datetime.timedelta(seconds=-1)#值跳转为最大-1 day, 23:59:59
   print datetime.timedelta(seconds=3600*24)#1 day, 0:00:00
  
  #microseconds 毫秒取值范围0 <= microseconds < 1000000 超出后会进行跳变
   print datetime.timedelta(microseconds=-1)#-1 day, 23:59:59.999999
   print datetime.timedelta(microseconds=1000000)#0:00:01,超过999999后进1
   print datetime.timedelta(microseconds=1000001)#0:00:01.000001
   print datetime.timedelta(microseconds=0)#0:00:00
  
   #minutes 的取值范围为0 <= minutes <60,超出范围后进行跳变
   print datetime.timedelta(minutes=60)#1:00:00,超59进1
   print datetime.timedelta(minutes=-1)#-1 day, 23:59:00,小于0后减1
  
  #hours 的取值范围为0 <= hours <23,超出范围后进行跳变
   print datetime.timedelta(hours=24)#1 day, 0:00:00,超23后进1
   print datetime.timedelta(hours=-1)#-1 day, 23:00:00,小于0后减1
  
  #weeks 乘7后转换成day的值,使用day的取值范围进行计算
#    print datetime.timedelta(weeks=999999999/7+1)#OverflowError: days=1000000001; must have magnitude <= 999999999
 

TAG: Python python

 

评分:0

我来说两句

日历

« 2024-04-20  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 27971
  • 日志数: 9
  • 建立时间: 2017-04-14
  • 更新时间: 2017-05-07

RSS订阅

Open Toolbar