在Django / Python中如何减去两个日期? [英] How do I subtract two dates in Django/Python?
问题描述
无论如何,我需要弄清楚如何从Django datetime对象中减去到目前为止,我很惊人在谷歌和这里的堆栈。我知道PHP,但从来没有处理过OO编程,所以请原谅我的无知。这是我的模型的样子:
class Goal(models.Model):
goal_weight = models.DecimalField 目标重量,
max_digits = 4,
decimal_places = 1)
target_date = models.DateTimeField(目标日期到达目标)
set_date = models.DateTimeField(你什么时候设定你的目标?)
comments = models.TextField(blank = True)
def __unicode __(self):
return unicode(self.goal_weight)
class Weight(models.Model):
给定日期和时间的重量。
goal = models.ForeignKey(Goal)
weight = models.DecimalField(Current Weight,
max_digits = 4,
decimal_places = 1)
weight_date = models.DateTimeField(Weigh-In Date)
comment = models.TextField(blank = True)
def __unicode __(self):
return unicode(self.weight)
def recording_today(self)
return self.date.date ()== datetime.date.today()
有关如何在视图中进行的任何想法?非常感谢!
你可以直接减去日期,这将产生一个 datetime.timedelta
对象:
dt = weight_now.weight_date - weight_then.weight_date
一个 timedelta
对象有几天,秒和微秒的字段。从那里,你可以做适当的数学。例如:
hours = dt.seconds / 60/60#返回日期之间的小时数
weeks = dt .days / 7#日期之间的周数
I'm working on a little fitness tracker in order to teach myself Django. I want to graph my weight over time, so I've decided to use the Python Google Charts Wrapper. Google charts require that you convert your date into a x coordinate. To do this I want to take the number of days in my dataset by subtracting the first weigh-in from the last weigh-in and then using that to figure out the x coords (for example, I could 100 by the result and increment the x coord by the resulting number for each y coord.)
Anyway, I need to figure out how to subtract Django datetime objects from one another and so far, I am striking out on both google and here at the stack. I know PHP, but have never gotten a handle on OO programming, so please excuse my ignorance. Here is what my models look like:
class Goal(models.Model):
goal_weight = models.DecimalField("Goal Weight",
max_digits=4,
decimal_places=1)
target_date = models.DateTimeField("Target Date to Reach Goal")
set_date = models.DateTimeField("When did you set your goal?")
comments = models.TextField(blank=True)
def __unicode__(self):
return unicode(self.goal_weight)
class Weight(models.Model):
""" Weight at a given date and time. """
goal = models.ForeignKey(Goal)
weight = models.DecimalField("Current Weight",
max_digits=4,
decimal_places=1)
weigh_date = models.DateTimeField("Date of Weigh-In")
comments = models.TextField(blank=True)
def __unicode__(self):
return unicode(self.weight)
def recorded_today(self):
return self.date.date() == datetime.date.today()
Any ideas on how to proceed in the view? Thanks so much!
You can just subtract the dates directly, which will yield a datetime.timedelta
object:
dt = weight_now.weight_date - weight_then.weight_date
A timedelta
object has fields for days, seconds, and microseconds. From there, you can just do the appropriate math. For example:
hours = dt.seconds / 60 / 60 # Returns number of hours between dates
weeks = dt.days / 7 # number of weeks between dates
这篇关于在Django / Python中如何减去两个日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!