Django - 计算之间的日期

时间:2010-05-31 11:22:44

标签: python django

我的数据库中有很多包含日期时间字段的记录(例如2010-05-23 17:45:57) 我想计算所有记录,例如: 15:00和15:59(这一切都可以从其他日期,月份或年份开始)。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

您可以将日期时间字段转换为Julian Dates 然后进行直接比较:

# Input is a list with the time of all the records, t_record_all

# Loop over all the records 
counter = 0
jd_low = ... #(this is your lower time limit in JD)
jd_hi = ... # (this is your higher time limit in JD)

for t_record in t_record_all:
    # Convert the time to julian date format
    jd_record = ... #(do your conversion here)
    if (jd_low <= jd_record <= jd_hi):
        # increment record counter
        counter = counter + 1
print counter