2014-11-03 36 views
4

有沒有比較Jinja中兩個日期的具體方法?如何比較Jinja2中的兩個日期?

我已經google搜索,但發現接近沒有任何與這個具體問題有關。

我已經找到最接近的事是這樣的,從官方的Jinja2文檔:

It is also possible to sort by an attribute (for example to sort by the date of an object) by specifying the attribute parameter: 

{% for item in iterable|sort(attribute='date') %} 
    ... 
{% endfor %} 
+0

你應該能夠比較它們,就像你在Python中的'set','if'塊一樣 – adarsh 2014-11-06 13:37:35

+0

你想排序一個列表或創建一個if語句嗎? – 2015-09-08 22:38:59

回答

1

我不知道你在找什麼,但是這是你如何可以比較一個日期來datetime.now( )。

我發現一個DateTime對象可以創建一個新的datetime對象:

In [1]: import datetime 

In [2]: wasNow = datetime.datetime.now() 

In [3]: wasNow 
Out[3]: datetime.datetime(2017, 7, 28, 14, 17, 21, 889530) 

In [4]: wasNow.now() 
Out[4]: datetime.datetime(2017, 7, 28, 14, 17, 30, 105077) 

,現在你有在神社模板,你能夠從現有的創建一個新的datetime對象的日期對象並比較它:

{% if item.date < item.date.now() %} 
    <p> This will display if the item.date is before item.date.now(). </p> 
{% endif %} 

我希望這可以幫助你在試圖比較日期使用jinja。