开发者

Django - count date between

开发者 https://www.devze.com 2023-01-01 09:36 出处:网络
I have many record in my database which contains datetime field (e.g. 2010-05-23 17:45:57). I want to count all records betwee开发者_如何学JAVAn e.g. 15:00 and 15:59 (it all can by from other day, mon

I have many record in my database which contains datetime field (e.g. 2010-05-23 17:45:57).

I want to count all records betwee开发者_如何学JAVAn e.g. 15:00 and 15:59 (it all can by from other day, month or year). How can I do this?


You can convert the datetime fields into Julian Dates and then do a straight comparison:

# 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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号