python time时间模块

在Python中,通常有这三种方式来表示时间:时间戳、元组(struct_time)、格式化的时间字符串

(1)时间戳(timestamp) :通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量。我们运行“type(time.time())”,返回的是float类型。

(2)时间元组(struct_time): struct_time元组共有9个元素共九个元素:(年,月,日,时,分,秒,一年中第几周,一年中第几天等)

(3)格式化的时间字符串(Format String): ‘1999-12-06’   ‘%Y-%m-%d %X'等等

导入时间模块
>>>import time

#时间戳
>>>time.time()
1500875844.800804

#时间字符串
>>>time.strftime("%Y-%m-%d %X")
'2017-07-24 13:54:37'
>>>time.strftime("%Y-%m-%d %H-%M-%S")
'2017-07-24 13-55-04'

#时间元组:localtime将一个时间戳转换为当前时区的struct_time
time.localtime()
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=24,
          tm_hour=13, tm_min=59, tm_sec=37, 
                 tm_wday=0, tm_yday=205, tm_isdst=0)

小结:时间戳是计算机能够识别的时间;时间字符串是人能够看懂的时间;元组则是用来操作时间的

#时间戳-->结构化时间
#time.gmtime(时间戳)    #UTC时间,与英国伦敦当地时间一致
#time.localtime(时间戳) #当地时间。例如我们现在在北京执行这个方法:与UTC时间相差8小时,UTC时间+8小时 = 北京时间 
>>>time.gmtime(1500000000)
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=2, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)
>>>time.localtime(1500000000)
time.struct_time(tm_year=2017, tm_mon=7, tm_mday=14, tm_hour=10, tm_min=40, tm_sec=0, tm_wday=4, tm_yday=195, tm_isdst=0)

#结构化时间-->时间戳 
#time.mktime(结构化时间)
>>>time_tuple = time.localtime(1500000000)
>>>time.mktime(time_tuple)
1500000000.0
#结构化时间-->字符串时间
#time.strftime("格式定义","结构化时间")  结构化时间参数若不传,则显示当前时间
>>>time.strftime("%Y-%m-%d %X")
'2017-07-24 14:55:36'
>>>time.strftime("%Y-%m-%d",time.localtime(1500000000))
'2017-07-14'

#字符串时间-->结构化时间
#time.strptime(时间字符串,字符串对应格式)
>>>time.strptime("2017-03-16","%Y-%m-%d")
time.struct_time(tm_year=2017, tm_mon=3, tm_mday=16, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=75, tm_isdst=-1)
计算时间差:
import time
true_time=time.mktime(time.strptime('2017-09-11 08:30:00','%Y-%m-%d %H:%M:%S'))
time_now=time.mktime(time.strptime('2017-09-12 11:00:00','%Y-%m-%d %H:%M:%S'))
dif_time=time_now-true_time
struct_time=time.gmtime(dif_time)
print('过去了%d年%d月%d天%d小时%d分钟%d秒'%(struct_time.tm_year-1970,struct_time.tm_mon-1,
                                       struct_time.tm_mday-1,struct_time.tm_hour,
                                       struct_time.tm_min,struct_time.tm_sec))

获取当前时间日月

# 今天日期   06.15
timer = time.strftime("%m.%d")
print(timer)   
# 昨天日期   06.14
timer = (date.today() + timedelta(days=-1)).strftime("%m.%d")
print(timer)
#  将时间字符串转化为datetime类型
start_date = datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")

获取本周,上周,本月,上月,本季的时间

import datetime
from datetime import timedelta
  
now = datetime.datetime.now()
# 今天 today = now print('--- today = {}'.format(today)) # 昨天 yesterday = now - timedelta(days=1) print('--- yesterday = {}'.format(yesterday)) # 明天 tomorrow = now + timedelta(days=1) print('--- tomorrow = {}'.format(tomorrow)) # 当前季度 now_quarter = now.month / 3 if now.month % 3 == 0 else now.month / 3 + 1 print('--- now_quarter = {}'.format(now_quarter)) # 本周第一天和最后一天 this_week_start = now - timedelta(days=now.weekday()) this_week_end = now + timedelta(days=6 - now.weekday()) print('--- this_week_start = {} this_week_end = {}'.format(this_week_start, this_week_end)) # 上周第一天和最后一天 last_week_start = now - timedelta(days=now.weekday() + 7) last_week_end = now - timedelta(days=now.weekday() + 1) print('--- last_week_start = {} last_week_end = {}'.format(last_week_start, last_week_end)) # 本月第一天和最后一天 this_month_start = datetime.datetime(now.year, now.month, 1) this_month_end = datetime.datetime(now.year, now.month + 1, 1) - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59) print('--- this_month_start = {} this_month_end = {}'.format(this_month_start, this_month_end)) # 上月第一天和最后一天 last_month_end = this_month_start - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59) last_month_start = datetime.datetime(last_month_end.year, last_month_end.month, 1) print('--- last_month_end = {} last_month_start = {}'.format(last_month_end, last_month_start)) # 本季第一天和最后一天 month = (now.month - 1) - (now.month - 1) % 3 + 1 this_quarter_start = datetime.datetime(now.year, month, 1) this_quarter_end = datetime.datetime(now.year, month + 3, 1) - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59) print('--- this_quarter_start = {} this_quarter_end = {}'.format(this_quarter_start, this_quarter_end)) # 上季第一天和最后一天 last_quarter_end = this_quarter_start - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59) last_quarter_start = datetime.datetime(last_quarter_end.year, last_quarter_end.month - 2, 1) print('--- last_quarter_start = {} last_quarter_end = {}'.format(last_quarter_start, last_quarter_end)) # 本年第一天和最后一天 this_year_start = datetime.datetime(now.year, 1, 1) this_year_end = datetime.datetime(now.year + 1, 1, 1) - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59) print('--- this_year_start = {} this_year_end = {}'.format(this_year_start, this_year_end)) # 去年第一天和最后一天 last_year_end = this_year_start - timedelta(days=1)+ datetime.timedelta( hours=23, minutes=59, seconds=59) last_year_start = datetime.datetime(last_year_end.year, 1, 1) print('--- last_year_start = {} last_year_end = {}'.format(last_year_start, last_year_end))

  

 

posted @ 2018-08-24 14:17  傻白甜++  阅读(320)  评论(0编辑  收藏  举报
TOP