简体   繁体   中英

Django - count date between

I have many record in my database which contains datetime field (eg 2010-05-23 17:45:57).
I want to count all records between eg 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 intoJulian 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM