python之time和datetime模块

  • 时间:
  • 浏览:2

a = datetime.date.today()print(a.timetuple())>>>time.struct_time(tm_year=2018, tm_mon=10, tm_mday=18, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=291, tm_isdst=-1)

datetime.time.utcoffset()

返回时区的时间偏移量:略

附一:

image.png

print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))>>>2018-10-18 19:21:13

time类有1个参数,datetime.time(hour,minute,second,microsecond,tzoninfo)

print(datetime.time(8,5,32))print(datetime.time(8,5,32,32))>>>08:05:32>>>08:05:32.000032

print(time.strptime("2018-10-18 15:56:42","%Y-%m-%d %H:%M:%S"))>>>time.struct_time(tm_year=2018, tm_mon=10, tm_mday=18, tm_hour=15, tm_min=56, tm_sec=42, tm_wday=3, tm_yday=291, tm_isdst=-1)

print(time.mktime(time.localtime()))>>>1539849828.0 注意和time.time()的区别:

print(time.strftime("%Y-%m-%d %H:%M:%S"))>>>2018-10-18 15:56:42

image.png

print(datetime.date.today())>>>2018-10-18

print(time.time())>>>1539847769.6119404

print(time.localtime())>>>time.struct_time(tm_year=2018, tm_mon=10, tm_mday=18, tm_hour=15, tm_min=35, tm_sec=0, tm_wday=3, tm_yday=291, tm_isdst=0)

print(time.time())1539849828.090303

image.png

datetime.time.tzname()

返回时区名字:略

print(datetime.datetime.now())>>>2018-10-18 19:15:17.691425

cls.replace()

替换八时 秒

a= datetime.time(8,5,32,32)print(a.replace(hour=18))>>>18:05:32.000032

print(time.gmtime(time.time()))>>>time.struct_time(tm_year=2018, tm_mon=10, tm_mday=18, tm_hour=8, tm_min=10, tm_sec=16, tm_wday=3, tm_yday=291, tm_isdst=0)

a = datetime.date.today()print(datetime.date.isoweekday(a))>>>4

醉里挑灯看剑,梦回吹角连营。八百里分麾下炙,五十弦翻塞外声。沙场秋点兵。

马作的卢很快,弓如霹雳弦惊。了却君王天下事,赢得生前肩上名。可怜白位于!

print(datetime.datetime.now().time())>>>19:16:54.497104

print(datetime.date.fromtimestamp(1539847769))>>>2018-10-18

datetime模块类的使用大同小异,有全都法律方式亲们不可能 这辈子都用不上,datetime类的法律方式比较多,在这里我就捡重点的列一下:

datetime.datetime.timedelta用于计算有一1个日期之间的差值:

附二:time时间之间的转换

print(datetime.datetime.now().date())>>>2018-10-18

a = datetime.date.today()print(datetime.date.weekday(a))>>>3

a = datetime.date.today()print(a.replace(year=2010))>>>2010-10-18

a = datetime.datetime.strptime("2018-10-18 19:21:37",'%Y-%m-%d %H:%M:%S')print(a,type(a))>>>2018-10-18 19:21:37 <class 'datetime.datetime'>

datetime模块定义了1个类,分别是

print(datetime.date(2017,12,1))>>>2017-12-01

cls.strftime("时间格式")

指定时间格式

a= datetime.time(8,5,32,32)print(a.strftime("%H:%M"))>>>08:05