美文网首页
Python时间处理

Python时间处理

作者: 一刀YiDao | 来源:发表于2016-08-14 17:10 被阅读97次

    1.Python有三个时间处理的模块datetime,date,time.

    1.1 datetime、date、time都提供了strftime()方法格式化strftime()

    格式化 解释 格式化 解释
    %y 两位数的年份表示(00-99) %B 本地完整的月份名称
    %Y 四位数的年份表示(000-9999) %c 本地相应的日期表示和时间表示
    %m 月份(01-12) %j 年内的一天(001-366)
    %d 月内中的一天(0-31) %p 本地A.M.或P.M.的等价符
    %H 24小时制小时数(0-23) %U 一年中的星期数(00-53)星期天为星期的开始
    %I 12小时制小时数(01-12) %w 星期(0-6),星期天为星期的开始
    %M 分钟数(00=59) %W 一年中的星期数(00-53)星期一为星期的开始
    %S 秒(00-59 ) %x 本地相应的日期表示
    %a 本地简化星期名称 %X 本地相应的时间表示
    %A 本地完整星期名称 %Z 当前时区的名称
    %b 本地简化的月份名称 %% %号本身

    1.2 常见日期输出方式

    1.2.1 datetime

    In [15]:import datetime
    
    In [16]:datetime.datetime.now()
    Out[16]: datetime.datetime(2016, 8, 14, 10, 34, 24, 230000)
    

    1.2.2 timestamp

    In [17]:import time
    
    In [18]:time.time()
    Out[18]: 1471142250.306
    

    1.2.3 time tuple

    In [19]:import time
    
    In [20]:time.localtime()
    Out[20]: time.struct_time(tm_year=2016, tm_mon=8, tm_mday=14, 
                tm_hour=10, tm_min=38, tm_sec=42, tm_wday=6, tm_yday=227, tm_isdst=0)
    

    1.2.4 string

    In [21]:import datetime
    
    In [22]:datetime.datetime.now().strftime("%Y-%m-%d %H:%M%S")
    Out[22]: '2016-08-14 10:5115'
    

    1.2.5 date

    In [23]:import datetime
    
    In [24]:datetime.datetime.now().date()
    Out[24]: datetime.date(2016, 8, 14)
    

    2.1 datetime基本操作

    2.1.1 获取当前datetime

    In [25]:import datetime
    
    In [26]:datetime.datetime.now()
    Out[26]: datetime.datetime(2016, 8, 14, 10, 57, 27, 783000)
    

    2.1.2 获取当前date

    In [27]:import datetime
    
    In [28]:datetime.date.today()
    Out[28]: datetime.date(2016, 8, 14)
    

    2.1.3 获取明天/前N天

    In [29]:import datetime
    
    In [30]:datetime.date.today()+datetime.timedelta(days=1)
    Out[30]: datetime.date(2016, 8, 15)
    

    2.1.4 三天前

    In [31]:import datetime
    
    In [32]:datetime.date.today()-datetime.timedelta(days=3)
    Out[32]: datetime.date(2016, 8, 11)
    

    2.1.5 获取当天开始和结束时间(00:00:00 23:59:59)

    In [33]:datetime.datetime.combine(datetime.date.today(), datetime.time.min)
    Out[33]: datetime.datetime(2016, 8, 14, 0, 0)
    
    In [34]:datetime.datetime.combine(datetime.date.today(), datetime.time.max)
    Out[34]: datetime.datetime(2016, 8, 14, 23, 59, 59, 999999)
    
    

    2.1.6 获取两个datetime的时间差

    In [36]:(datetime.datetime(2017,1,13,12,0,0)-datetime.datetime.now())
    Out[36]: datetime.timedelta(152, 3104, 273000)
    
    
    In [37]:(datetime.datetime(2017,1,13,12,0,0)-datetime.datetime.now()).total_seconds()
    Out[37]: 13135887.501
    
    

    2.1.7 获取本周/本月/上月最后一天

    #本周
    In [40]:today = datetime.date.today()
    
    In [40]:today
    Out[40]: datetime.date(2016, 8, 14)
    
    In [41]:sunday = today+datetime.timedelta(6-today.weekday())
    
    In [41]:sunday
    Out[42]: datetime.date(2016, 8, 14)
    
    #本月
    In [43]:import calendar
    
    In [44]:today = datetime.date.today()
    In [45]: today
    Out [45]: today
     (0, 31)
    In [46]:last_day_num = calendar.monthrange(today.year, today.month)
    
    In [47]:last_day = datetime.date(today.year, today.month, last_day_num[1])
    Out[48]: datetime.date(2016, 8, 31)
    
    #获取上个月的最后一天(可能跨年)
    In [49]:import datetime
    In [50]:today = datetime.date.today()
    In [51]:today
    Out[51]: datetime.date(2016, 8, 14)
    
    In [52]:first = datetime.date(day=1,month=today.month,year=today.year)
    In [53]:first
    Out[53]: datetime.date(2016, 8, 1)
    
    In [54]:lastMonth = first-datetime.timedelta(days=1)
    In [55]:lastMonth
    Out[55]: datetime.date(2016, 7, 31)
    

    3 关系之间的转换 Datetime / String / timestamp / time tuple

    3.1datetime <=> string

    3.1.1 datetime -> string

    In [56]:import datetime
    
    In [57]:datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
    Out[57]: '2016-08-14 17:26:42'
    

    3.1.2 string -> datetime

    In [72]:import datetime
    In [73]:datetime.datetime.strptime("2016-08-14 12:35:57","%Y-%m-%d %H:%M:%S")
    Out[73]: datetime.datetime(2016, 8, 14, 12, 35, 57)
    

    3.2 datetime <=> timetuple

    3.2.1 datetime -> timetuple

    In [74]:import datetime
    In [75]:datetime.datetime.now().timetuple()
    Out[75]: time.struct_time(tm_year=2016, tm_mon=8, tm_mday=14, tm_hour=17, 
           tm_min=31, tm_sec=58, tm_wday=6, tm_yday=227, tm_isdst=-1)
    

    3.3 datetime <=> date

    3.3.1 datetime -> date

    In [76]:import datetime
    In [77]:datetime.datetime.now().date()
    Out[77]: datetime.date(2016, 8, 14)
    

    3.3.2 datetime -> date

    In [78]:import datetime
    In [79]:datetime.date.today()
    Out[79]: datetime.date(2016, 8, 14)
    
    In [80]:today = datetime.date.today()
    today
    Out[80]: datetime.date(2016, 8, 14)
    
    In [81]:datetime.datetime.combine(today,datetime.time())
    Out[81]: datetime.datetime(2016, 8, 14, 0, 0)
    
    In [82]:datetime.datetime.combine(today,datetime.time.min)
    Out[82]: datetime.datetime(2016, 8, 14, 0, 0)
    

    3.4 datetime <=> timestamp

    3.4.1datetime -> timestamp

    In [83]:import datetime
    In [84]:now = datetime.datetime.now()
    In [85]:now
    Out[85]: datetime.datetime(2016, 8, 14, 17, 40, 44, 757000)
    In [86]:timestamp = time.mktime(now.timetuple())
    In [87]:timestamp
    Out[87]: 1471167644.0
    

    3.4.2 timestamp -> datetime

    In [88]:import datetime
    In [89]:datetime.datetime.fromtimestamp(1421077403.0)
    Out[89]: datetime.datetime(2015, 1, 12, 23, 43, 23)
    

    学习内容来自网站

    相关文章

      网友评论

          本文标题:Python时间处理

          本文链接:https://www.haomeiwen.com/subject/aeeysttx.html