在使用Python开发过程中,除了使用datetime标准库处理时间日期外,还有很多第三方开源库值得尝试。1.ArrowArrow是一个专门处理时间和日期的轻量级Python库。它提供了一种合理且智能的方式来创建、操作、格式化和转换时间和日期,并为许多常见的构建场景提供支持。智能模块API。简而言之,它可以帮助您使用更简单的操作和更少的代码来使用日期和时间。它的设计灵感主要来自moment.js和requests。快速启动$pipinstallarrow>>>importarrow>>>utc=arrow.utcnow()>>>utc>>>utc=utc.replace(小时=-1)>>>utc>>>local=utc.to('US/Pacific')>>>local>>>arrow.get('2013-05-11T21:23:58.970460+00:00')<箭头[2013-05-11T21:23:58.970460+00:00]>>>>local.timestamp1368303838>>>local.format()'2013-05-1113:23:58-07:00'>>>local.format('YYYY-MM-DDHH:mm:ssZZ')'2013-05-1113:23:58-07:00'>>>local.humanize()'anhourago'>>>local.humanize(locale='ko_kr')'1???'2,提供byDeloreanDelorean比datetime和pytz更好的抽象,让你更容易处理时间。它有许多有用的功能,可用于处理时区、标准化时区或从一个时区更改为另一个时区。快速启动fromdatetimeimportdatetimeimportpytzest=pytz.timezone('US/Eastern')d=datetime.now(pytz.utc)d=est.normalize(d.astimezone(est))returndfromdeloreanimportDeloreand=Delorean()d=d.shift('US/eastern')returnd3,Pendulum原生的datetime对于基本情况已经足够了,但是当面对更复杂的用例时,它通常会捉襟见肘,而且不那么直观。Pendulum在标准库的基础上,提供了更简洁易用的API,旨在让Pythondatetime更易用。快速启动>>>importpendulum>>>now_in_paris=pendulum.now('Europe/Paris')>>>now_in_paris'2016-07-04T00:49:58.502116+02:00'#Seamlesstimezonesswitching>>>now_in_paris.in_timezone('UTC')'2016-07-03T22:49:58.502116+00:00'>>>tomorrow=pendulum.now().add(days=1)>>>last_week=pendulum.now().subtract(weeks=1)>>>ifpendulum.now().is_weekend():...print('Party!')'Party!'>>>past=pendulum.now().subtract(minutes=2)>>>过去.diff_for_humans()>>>'2minutesago'>>>delta=past-last_week>>>delta.hours23>>>delta.in_words(locale='en')'6days23hours58minutes'#Properhandlingofdatetimenormalization>>>pendulum.create(2013,3,31,2,30,0,0,'欧洲/巴黎')'2013-03-31T03:30:00+02:00'#2:30doesnotexist(Skippedtime)#Properhandlingofdsttransitions>>>just_before=钟摆。创建(2013,3,31,1,59,59,999999,'欧洲/巴黎')'2013-03-31T01:59:59.999999+01:00'>>>just_before.add(microseconds=1)'2013-03-31T03:00:00+02:00'4.dateutildateutil是datetime标准库的扩展库,支持对几乎所有字符串格式的日期进行通用解析,灵活的日期计算和及时的内部数据更新快速启动>>>fromdateutil.relativedeltaimport*>>>fromdateutil.easterimport*>>>fromdateutil.rruleimport*>>>fromdateutil.parserimport*>>>fromdatetimeimport*>>>now=parse("SatOct1117:13:46UTC2003")>>>today=now.date()>>>year=rrule(YEARLY,dtstart=now,bymonth=8,bymonthday=13,byweekday=FR)[0].year>>>rdelta=relativedelta(复活节(年),today)>>>print("Todayis:%s"%today)Todayis:2003-10-11>>>print("YearwithnextAug13thonaFridayis:%s"%year)YearwithnextAug13thonaFridayis:2004>>>print("HowfaristheEasterofthatyear:%s"%rdelta)HowfaristheEasterofthatyear:relativedelta(months=+6)>>>print("AndtheEasterofthatyearis:%s"%(today+rdelta))AndtheEasterofthatyearis:2004-04-115,momentPython用于处理日期/时间库,设计灵感同样来源于moment.js和requests,设计理念来源于TimesPython模块。Usageimportmomentfromdatetimeimportdatetime#Createamomentfromastringmoment.date("12-18-2012")#Createamomentwithaspecifiedstrftimeformatmoment.date("12-18-2012","%m-%d-%Y")#Momentusestheawesomedateparserlibrarybehindthescenesmoment.date("2012-12-18")#Createamomentwithwordsinitmoment.date("December18,2012")#Createamomentthatwouldnormallybeprettyhardtodomoment.date("2weeksago")#Createafuturemomentthatwouldotherwisebereallydifficultmoment.date("2weeksfromnow")#Createamomentfromthecurrentdatetimemoment.now()#ThemomentcanalsobeUTC-basedmoment.utcnow()#CreateamomentwiththeUTCtimezonemoment.utc("2012-12-18“)#createAmentFromaNixTimestAmpMoment.unix(13555875153626)#createAmentFromaNixutctimestampMoment.unix(13555875153626,13555875153626,utc=true)日期#CreateandformamomentusingMoment.jssemanticsmoment.now().format("YYYY-M-D")#Createandformatamomentwithstrftimesemanticsmoment.date(2012,12,18).strftime("%Y-%m-%d")#Updateyourmoment'stimezonemoment.date(datetime(2012,12,18)).locale("US/Central").date#Alterthemoment'sUTCtimezonetoadifferenttimezonemoment.utcnow().timezone("US/Eastern").date#Setandupdateyourmoment'stimezone.Forinstance,I'monthe#westcoast,butwantNYC'currenttime.moment.now().locale("US/Pacific").timezone("US/Eastern")#Inordertomanipulatetimezones,alocalemustwaysbesetor#youmustbeusingUTC.moment.utcnow().timezone("US/Eastern").date#Youcanalsocloneamoment,sotheoriginalstaysunalterednow=moment.utcnow().timezone("US/Pacific")future=now.clone().add(weeks=2)6.When.py提供了非常人性化的功能来帮助执行常见的Date和时间操纵用法