本文介绍了如何检查给定的datetime对象是否在“之间”两个datetimes?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  my_event = Event.objects.get(id = 4)
current_time = datetime.datetime.now()
pre>

如何检查我当前的时间是否在他们之间?

  my_event.start_time< current_time< my_event.end_time 


解决方案

您的答案是以只要start_time和end_time没有关联的tzinfo类。你不能直接比较一个天真的datetime和timezoned-datetime。


my_event = Event.objects.get(id=4)
current_time = datetime.datetime.now()

How do I do check if my current time is between them?

my_event.start_time < current_time < my_event.end_time
解决方案

Your answer is the way to go as long as start_time and end_time don't have an associated tzinfo class. You can't directly compare a naive datetime with a timezoned-datetime.

这篇关于如何检查给定的datetime对象是否在“之间”两个datetimes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 17:08