somepeople

python模块之time&datetime模块

TIME:

1.时间戳:

print(time.time())
#1595852043.5159092

1.struc_time:

  本地时间

print(time.localtime())
#time.struct_time(tm_year=2020, tm_mon=7, tm_mday=27, tm_hour=20, tm_min=14, tm_sec=34, tm_wday=0, tm_yday=209, tm_isdst=0)

  格林威治时间(0时区)

print(time.gmtime())
#time.struct_time(tm_year=2020, tm_mon=7, tm_mday=27, tm_hour=12, tm_min=15, tm_sec=37, tm_wday=0, tm_yday=209, tm_isdst=0)

3.格式化时间

print(time.strftime('%Y-%m-%d %H:%M',time.localtime()))
print(time.strptime('2020-07-27 20:08','%Y-%m-%d %H:%M'))
#2020-07-27 20:09
#time.struct_time(tm_year=2020, tm_mon=7, tm_mday=27, tm_hour=20, tm_min=8, tm_sec=0, tm_wday=0, tm_yday=209, tm_isdst=-1)

4.struc_time转换成时间戳

print(time.mktime(time.localtime()))
#1595852194.0

5.默认格式化时间

print(time.ctime(time.time()))     #时间戳
print(time.asctime(time.localtime()))          #struc_time
#Mon Jul 27 20:19:42 2020
#Mon Jul 27 20:19:42 2020

 DATETIME:

1.当前时间:

print(datetime.datetime.now())
#2020-07-27 20:31:34.453467
print(datetime.datetime.now().date())
#2020-07-27
print(datetime.datetime.now().time())
#20:31:34.454467
print(datetime.datetime.now().ctime())
#Mon Jul 27 20:31:34 2020            
print(datetime.datetime.now()+datetime.timedelta(hours=12))
#2020-07-28 08:31:34.454467           #通过运算修改时间
print(datetime.date.fromtimestamp(233233424))
#1977-05-23                     #时间戳转换为时间
print(datetime.datetime.now().replace(year=2020,month=12,day=31))
#2020-12-31 20:35:05.444832                  #时间替换

 

posted on 2020-07-27 20:21  Hello_Thanos  阅读(171)  评论(0编辑  收藏  举报

导航