Python 判斷時(shí)間是否在時(shí)間區(qū)間內(nèi)的實(shí)例
判斷時(shí)間是否在時(shí)間區(qū)間內(nèi)
大家都知道 3<4<5這種連等式判斷在python中是可行的
>>> 3<4<5True
那么給定時(shí)間是否在時(shí)間區(qū)間內(nèi),也可以用連等式來(lái)判斷
# 給定兩個(gè)時(shí)間來(lái)比較下>>> from datetime import datetime>>> a=datetime.now()>>> b=datetime.now()>>> adatetime.datetime(2019, 6, 5, 14, 3, 28, 396610)>>> bdatetime.datetime(2019, 6, 5, 14, 3, 35, 226784)>>> a<bTrue>>> b<aFalse
發(fā)現(xiàn)結(jié)果如預(yù)期
from dateutil.parser import parseNOW = datetime.now()gt = parse('2019-6-3')lt = parse('2019-6-8')gt<NOW<lt
擴(kuò)展:
隨著業(yè)務(wù)越來(lái)越復(fù)雜,上面簡(jiǎn)單的比較已經(jīng)不能解決問(wèn)題,后邊用到了區(qū)間比較的庫(kù)
from interval import Intervala = Interval(s1, e1)b = Interval(s2, e2)a in b
時(shí)間字符串比較
datetime比較
補(bǔ)充知識(shí):判斷當(dāng)前時(shí)間是否在[startTime, endTime]區(qū)間
我就廢話不多說(shuō)了,大家還是直接看代碼吧
/** * 判斷當(dāng)前時(shí)間是否在[startTime, endTime]區(qū)間,注意時(shí)間格式要一致 * * @param nowTime 當(dāng)前時(shí)間 * @param startTime 開始時(shí)間 * @param endTime 結(jié)束時(shí)間 */ public static boolean isEffectiveDate(Date nowTime, Date startTime, Date endTime) { if (nowTime.getTime() == startTime.getTime()|| nowTime.getTime() == endTime.getTime()) { return true; } Calendar date = Calendar.getInstance(); date.setTime(nowTime); Calendar begin = Calendar.getInstance(); begin.setTime(startTime); Calendar end = Calendar.getInstance(); end.setTime(endTime); if (date.after(begin) && date.before(end)) { return true; } else { return false; } }
以上這篇Python 判斷時(shí)間是否在時(shí)間區(qū)間內(nèi)的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python如何批量生成和調(diào)用變量2. 通過(guò)CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效3. .net如何優(yōu)雅的使用EFCore實(shí)例詳解4. Python獲取B站粉絲數(shù)的示例代碼5. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖6. python基礎(chǔ)之匿名函數(shù)詳解7. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車8. ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析9. python b站視頻下載的五種版本10. ASP.Net Core(C#)創(chuàng)建Web站點(diǎn)的實(shí)現(xiàn)
