欢迎各位兄弟 发布技术文章
这里的技术是共享的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #coding:utf-8import datetimeprint datetime.time()t = datetime.time(1, 3, 5, 25)print tprint t.hour #时print t.minute #分print t.second #秒print t.microsecond #毫秒print datetime.time.max #一天的结束时间print datetime.time.min #一天的开始时间######运行结果######>>> 00:00:0001:03:05.0000251352523:59:59.99999900:00:00 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | #coding:utf-8import datetime#设置日期t = datetime.date(2013, 2, 3)#打印设置日期的和元组print t.timetuple()#日期元组print tprint t.year #年print t.month #月print t.day #日#获取今天的日期today = datetime.date.today()print todayprint datetime.datetime.now()#这个打印到毫秒级别#获取今天日期的元组t1 = today.timetuple()print t1#打印成ctime格式(time.ctime()格式)#'%a %b %d %H:%M:%S %Y'print t.ctime()print today.ctime()######运行结果######>>> time.struct_time(tm_year=2013, tm_mon=2, tm_mday=3, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=34, tm_isdst=-1)2013-02-032013232013-07-282013-07-28 20:13:25.942000time.struct_time(tm_year=2013, tm_mon=7, tm_mday=28, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=209, tm_isdst=-1)Sun Feb 3 00:00:00 2013Sun Jul 28 00:00:00 2013 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | #coding:utf-8import datetime#打印:从毫秒到周的表示格式 = 转换成秒 (total_seconds()) for i in [datetime.timedelta(milliseconds=1), #1毫秒 datetime.timedelta(seconds=1), #1秒 datetime.timedelta(minutes=1), #1分钟 datetime.timedelta(hours=1), #1小时 datetime.timedelta(days=1), #1天 datetime.timedelta(weeks=1)]:#11周 #print i + ':' + i.total_seconds() print '%s = %s seconds'%(i,i.total_seconds())printprint '~' * 20 + '我是分割线' + '~' * 20print '计算时间的加减。。。。。。。。。'a = datetime.datetime.now()print '现在时间是:'print aprint '加5小时之后变成:'b = a + datetime.timedelta(hours=5)print bprint '加一周之后变成:'c = a + datetime.timedelta(weeks=1)print cprint '减去一周后变成:'d = a - datetime.timedelta(weeks=1)print dprint '计算2个时间相差多久'print '%s减去%s'%(b, a)print '等于:%s'%(b - a)print '%s减去%s'%(a, d)print '等于:%s'%(a - d)printprint '~' * 20 + '我是分割线' + '~' * 20print '比较2个时间:'print '比较当天和一周前的'print a > dprint '如果比较d > a 的话就返回False'printprint '~' * 20 + '我是分割线' + '~' * 20print '上面的列子都是把日期和时间分开的,现在我们来把他们自由结合'print '假设我们想要的时间是:2014-01-05 13:14:25't = datetime.time(13, 14, 25)d = datetime.date(2014, 01, 05)print datetime.datetime.combine(d, t)######运行结果######>>> 0:00:00.001000 = 0.001 seconds0:00:01 = 1.0 seconds0:01:00 = 60.0 seconds1:00:00 = 3600.0 seconds1 day, 0:00:00 = 86400.0 seconds7 days, 0:00:00 = 604800.0 seconds~~~~~~~~~~~~~~~~~~~~我是分割线~~~~~~~~~~~~~~~~~~~~计算时间的加减。。。。。。。。。现在时间是:2013-07-28 21:34:33.531000加5小时之后变成:2013-07-29 02:34:33.531000加一周之后变成:2013-08-04 21:34:33.531000减去一周后变成:2013-07-21 21:34:33.531000计算2个时间相差多久2013-07-29 02:34:33.531000减去2013-07-28 21:34:33.531000等于:5:00:002013-07-28 21:34:33.531000减去2013-07-21 21:34:33.531000等于:7 days, 0:00:00~~~~~~~~~~~~~~~~~~~~我是分割线~~~~~~~~~~~~~~~~~~~~比较2个时间:比较当天和一周前的True如果比较d > a 的话就返回False~~~~~~~~~~~~~~~~~~~~我是分割线~~~~~~~~~~~~~~~~~~~~上面的列子都是把日期和时间分开的,现在我们来把他们自由结合假设我们想要的时间是:2014-01-05 13:14:252014-01-05 13:14:25 |
python使用datetime模块timedelta实现日期时间相加:
from datetime import datetime from datetime import timedelta now = datetime.now() aDay = timedelta(days=1) now = now + aDay print now.strftime('%Y-%m-%d')
from datetime import datetime from datetime import timedelta now = datetime.now() aDay = timedelta(days=-1) now = now + aDay print now.strftime('%Y-%m-%d')
使用timedelta可以很方便的在日期上做天days,小时hour,分钟,秒,毫秒,微妙的时间计算,如果要计算月份则需要另外的办法
import time last_month = time.localtime()[1]-1 or 12 print last_month
from datetime import timedelta from datetime import datetime import time d1 = datetime.now() time.sleep(10) eclipseTimes = datetime.now() - d1 print eclipseTimes.total_seconds()