Python 3.x Python中的日期/时间加法/减法

Python 3.x Python中的日期/时间加法/减法,python-3.x,Python 3.x,我的代码是: from datetime import datetime, timedelta date_format = "%d/%m/%Y %H%M%S" a = datetime.strptime('19/11/2014 090000', date_format) b = datetime.strptime('25/11/2014 114736', date_format) delta = b - a del_sec = delta.seconds minutes = del_sec//6

我的代码是:

from datetime import datetime, timedelta
date_format = "%d/%m/%Y %H%M%S"
a = datetime.strptime('19/11/2014 090000', date_format)
b = datetime.strptime('25/11/2014 114736', date_format)
delta = b - a
del_sec = delta.seconds
minutes = del_sec//60
hours = minutes//60
print (delta.days,"day(s) " "%02d:%02d:%02d" " HH:MM:SS"% (hours, minutes % 60, del_sec % 60 ))
也可以包括天数;我需要一个变量“c”来存储当前时间,并以现有格式从“c”中减去“a”


我尝试了不同的组合,但它们不起作用。

使用strtime创建日期时间时,它没有时区信息。如果使用datetime.utcnow获取当前UTC时间,或者使用datetime.now获取计算机时区的当前datetime,则也是如此

不知道你在找什么。也许不是这样,但也许你可以说为什么它不是你想要的:

b = datetime.strptime('25/11/2014 054736', date_format)
c = datetime.now()
print(b)
print(c)
print(c - b)
2014-11-25 05:47:36 2014-11-25 06:08:41.797725

0:21:05.797725


这应足以满足大多数日期/时间要求:

//Code tested on Python 3.4 Windows 10 and Android libpython2.6 Android

//Include the following two lines if you want to run the code on Android
//import android
//droid = android.Android()

from datetime import datetime

date_format = "%d/%m/%Y %H%M%S"     // Used for a and b

a = datetime.strptime('23/11/2014 093000', date_format)
b = datetime.strptime('24/11/2014 093000', date_format)
c = datetime.now()
d= datetime.strptime('093000', '%H%M%S')        // Only for time comparison if someone need it.
e= datetime.strptime('093000', '%H%M%S')

delta = c - b                       // Difference between time(s); you can play with a,b and c here since they are in same format

gr=d==e                         // Comparison operator on time if needed can use (>, <, >=, <= and == etc.); time only
print(gr)

fr=c>=b                         // Comparison operator on time if needed can use (>, <, >=, <= and == etc.); date and time                  
print(fr)

days = delta.days

del_sec = delta.seconds
fsec = del_sec%60

minutes = del_sec//60
fmin = minutes%60

fhour = minutes//60

print("%02d:Day(s) %02d:Hour(s) %02d:Minute(s) %02d:Second(s)" %(days,fhour,fmin,fsec))

您希望c存储当前时间或当前日期和时间?当前日期和时间,以便我可以在几天后或同一天进行加减。您可以显示您已尝试的内容吗?部分原因是,我处于UTC+5:30区域;如何添加此偏移量。如果可以的话,这正是我想要的。正是!这就是我想要的。我不知道为什么我错过了这个。