python时间减法 [英] python time subtraction

查看:29
本文介绍了python时间减法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Python 中获取时间.time.ctime() 有很多功能:

I want to get the time in Python. With time.ctime(), there are lots of functions:

我试过了:

def write_time():  
  NUMBER_OF_MIN=40 #my offset
  obj=time.gmtime()
  print  " D", obj.tm_mday, " M",obj.tm_mon,  "Y",obj.tm_year, 
  " time", obj.tm_hour+TIME_OFFSET,":",   obj.tm_min-NUMBER_OF_MIN, ":",obj.tm_sec

我想从时间中减去 40 分钟.

I want to subtract 40 minutes from the time.

推荐答案

查看 datetime 库,它为使用日期的数学提供了更大的灵活性.

Check out the datetime library, which provides much more flexibility for math using dates.

例如:

import datetime
print datetime.datetime.now()
print datetime.datetime.now() - datetime.timedelta(minutes=2)
print datetime.datetime.now() - datetime.timedelta(seconds=10)
print datetime.datetime.now() - datetime.timedelta(milliseconds=400)

打印:

el@dev ~ $ python test.py
2014-11-26 06:47:07.179411
2014-11-26 06:45:07.179538
2014-11-26 06:46:57.179581
2014-11-26 06:47:06.779614

这篇关于python时间减法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆