2016-10-29 53 views
-1

我有以下代碼:Python的日期匹配與MySQL

fixtures = StraightredFixture.objects.filter(soccerseason=soccerseason,fixturematchday=fixturematchday).order_by('fixturedate') 

firstGameTime = str(fixtures[0].fixturedate).split() 
currentTime = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S")).split() 


if firstGameTime >= currentTime: 
    selectteams = True 
else: 
    selectteams = False 

無論firstGameTime的日期是它總是返回False。兩個例子如下:

firstGameTime = ['2010-10-28', '11:30:00+00:00'] 
currentTime = ['2016-10-29', '10:51:50'] 
selectteams = False 

firstGameTime = ['2010-10-30', '11:30:00+00:00'] 
currentTime = ['2016-10-29', '10:53:16'] 
selectteams = False 

在第二個例子中,我希望它說真正的firstGameTime是30號,而currentTime的是第29屆。

我有一種感覺,它是與+00:00部分做的,但在如何解決這個問題上有所損失。任何幫助將不勝感激,非常感謝,艾倫。

+0

你爲什麼將這些以字符串列表進行比較,而不是讓他們爲日期時間? –

+0

在某處作爲解決方案在線閱讀它。我不需要,所以任何解決方案表示讚賞。 –

回答

1

日期時間可直接比較。沒有必要轉換爲字符串列表。

firstGameTime = fixtures[0].fixturedate 
currentTime = datetime.now() 

if firstGameTime >= currentTime: 
    ... 

然而,因爲這是你的Django的查詢可以做到這一點直接:

selectteams = StraightredFixture.objects.filter(
    soccerseason=soccerseason, 
    fixturematchday=fixturematchday 
    fixturedate__gte=datetime.now() 
).exists() 
+0

不幸的是,我收到以下錯誤:無法比較偏移天真和偏移量感知日期時間。 –

+0

通過使用你的答案和在以下鏈接的幫助:http://stackoverflow.com/questions/15307623/cant-compare-naive-and-aware-datetime-now-challenge-datetime-end我得到了解決方案。非常感謝您的幫助。貶低我這個問題的人可否與我聯繫,幫助我瞭解downvote以備將來參考。 –