时间无疑是生活方方面面最关键的因素之一,因此记录和追踪时间就变得非常重要。在Python中,可以通过其内置库跟踪日期和时间。今天我们将介绍Python中的日期和时间,让我们一起学习如何使用time和datetime模块来查找和修改日期和时间。Python处理日期和时间的模块Python提供了time和datetime模块,可以帮助我们方便的获取和修改日期和时间。让我们一一看看。时间模块这个模块包括了执行各种时间操作所需的所有与时间相关的功能,它还使我们能够访问各种用途所需的时钟类型。内置函数:请看下表,它描述了时间模块的一些重要的内置函数。functionDescriptiontime()返回自纪元以来经过的秒数ctime()将经过的秒数作为参数并返回当前日期和时间sleep()停止线程执行给定持续时间time.struct_time类函数或class作为参数,要么将其作为输出返回localtime()将自纪元以来经过的秒数作为参数,并将日期和时间作为时间返回。struct_time格式gmtime()类似于localtime()并返回时间。UTC格式的struct_timemktime()ocaltime()的倒数。采用9个参数的元组并返回自纪元输出以来经过的秒数asctime()采用9个参数的元组并返回表示相同参数的字符串strftime()采用9个参数组的元组,并返回表示根据使用的格式代码相同的参数。strptime()解析字符串并及时返回。struct_timeformat代码格式化:在用例子解释每个函数之前,先看看所有格式化代码的合法方式:August%c本地日期时间版本TueAug231:31:402019%d表示本月第几天(01-31)07%fMicroseconds000000-999999%HHour(00-23)15%IHour(00-11)3%jDay年的235%m月份数(01-12)07%M分钟数(00-59)44%pAM/PMAM%S秒数(00-59)23%U从星期日开始的一年中的星期数(00-53)12%w星期数oftheweekMonday(1)%W从星期一开始的一年中的星期数(00-53)34%x本地日期06/07/22%X本地时间12:30:45%y年份(简版)22%Y年份(完整版)2022%zUTCoffset+0100%ZTimezoneCST%%%Character%struct_time类有如下属性:AttributeValuetm_year0000,..,2019,…,9999tm_mon1-12tm_mday1-31tm_hour0-23tm_min0-59tm_sec0day_day_is61t-366tm_isdst0,1,-1(夏令时)当未知own)现在让我们看几个时间模块的例子。使用时间模块查找日期和时间在Python中使用上表中描述的内置函数和格式代码很容易获取日期和时间。importtime#timea=time.time()#totalsecondssinceepochprint("Secondssinceepoch:",a,end='n--------n')#ctimeprint("当前日期和时间:")print(time.ctime(a),end='n--------n')#sleeptime.sleep(1)#执行会延迟一秒#localtimeprint("本地时间:")print(time.localtime(a),end='n--------n')#gmtimeprint("UTC格式的本地时间:")print(time.gmtime(a),end='n------------n')#mktimeb=(2019,8,6,10,40,34,1,218,0)print("当前时间秒数:")print(time.mktime(b),end='n--------n')#asctimeprint("本地格式的当前时间:")print(time.asctime(b),end='n-----------n')#strftimec=time.localtime()#getstruct_timed=time.strftime("%m/%d/%Y,%H:%M:%S",c)print("表示日期和时间的字符串:")print(d,end='n--------n')#strptimeprint("time.strptime解析字符串并以struct_time格式返回:n")e="2019年8月6日"f=time.strptime(e,"%d%B,%Y")print(f)输出:自纪元以来的秒数:1565070251.7134922————当前日期和时间:TueAug611:14:112019————本地时间:time.struct_time(tm_year=2019,tm_mon=8,tm_mday=6,tm_hour=11,tm_min=14,tm_sec=11,tm_wday=1,tm_yday=218,tm_isdst=0)————UTC格式的本地时间:time.struct_time(tm_year=2019,tm_mon=8,tm_mday=6,tm_hour=5,tm_min=44,tm_sec=11,tm_wday=1,tm_yday=218,tm_isdst=0)————CurrentTimeinseconds:1565068234.0————CurrentTimeinlocalformat:TueAug610:40:342019—————表示日期和时间的字符串:08/06/2019,11:14:12————time.strptime解析字符串并以struct_time格式返回:time.struct_time(tm_year=2019,tm_mon=8,tm_mday=6,tm_hour=0,tm_min=0,tm_sec=0,tm_wday=1,tm_yday=218,tm_isdst=-1)datetime模块类似于time模块,datetime模块包含了处理日期和时间所必需的所有方法内置函数:下表描述了该模块中的一些重要函数:年、月、日为参数,创建对应的日期time()以时、分、秒、微秒和tzinfo为参数,创建对应的日期)它是不同日期或时间之间的差异(持续时间)使用datetime查找日期和时间现在,让我们尝试使用datetime模块在Python中实现这些函数来查找日期和时间。importdatetime#datetimeconstructorprint("Datetimeconstructor:n")print(datetime.datetime(2019,5,3,8,45,30,234),end='n--------n')#todayprint("当前日期和时间使用今天:n")print(datetime.datetime.today(),end='n--------n')#nowprint("当前日期和时间使用今天:n")print(datetime.datetime.now(),end='n--------n')#dateprint("设置日期:n")print(datetime.date(2019,11,7),end='n--------n')#timeprint("设置时间:n")print(datetime.time(6,30,23),end='n----------n')#date.fromtimestampprint("将秒数转换为日期和时间:n")print(datetime.date.fromtimestamp(23456789),end='n----------n')#timedeltab1=datetime.timedelta(days=30,seconds=0,microseconds=0,milliseconds=0,minutes=0,hours=4,weeks=8)b2=datetime.timedelta(days=3,seconds=0,microseconds=0,milliseconds=0,minutes=0,hours=4,weeks=8)b3=b2-b1print(type(b3))print("结果duration=",b3,end='n--------n')#attributesa=datetime.datetime.now()#1print(a)print("年份是:",a.year)print("小时:",a.hour)Output:Datetimeconstructor:2019-05-0308:45:30.000234————当前dateandtimeusingtoday:2019-08-0613:09:56.651691————-当前日期时间使用今天:2019-08-0613:09:56.651691————-设置日期:2019-11-07————-设置时间:06:30:23————将秒数转换为日期时间:1970-09-29————-
