2015-05-21 58 views
1

我想到的是通過與當前日期申請多項選擇減去值計算日期的方式如何減去日期時間多選擇值

我有有值的多個選擇,我想減去模型與datetime.datetime.now,但我不知道它應該如何完成。

REMINDER_CHOICES = (
    (5,'5 minutes before'), 
    (15, '15 minutes before'), 
    (30, '30 minutes before'), 
    (60, '1 hour before'), 
    (120, '2 hours before'), 
    (240, '4 hours before'), 
    (1440, '1 day before'), 
) 

回答

3

就喜歡。減去這個timedelta:

date_b = date_a - datetime.timedelta(minutes=30) 

date_b = date_a - datetime.timedelta(days=2, minutes=30) 
2

你可以使用dateutil來添加和減去datetime。對於如..

import dateutil.relativedelta 
    datetime.datetime.now() + dateutil.relativedelta.relativedelta(day=5) 

如果你想接着。減去:

datetime.datetime.now() + dateutil.relativedelta.relativedelta(day=-5)