• time 模块


     

    1. time模块

    1.1测试

    >>> time.time()
    1523778095.6930795
    
    >>> time.localtime()
    time.struct_time(tm_year=2018, tm_mon=4, tm_mday=15, tm_hour=15, tm_min=41, tm_sec=45, tm_wday=6, tm_yday=105, tm_isdst=0)
    
    >>> time.localtime().tm_year
    2018
    >>> time.localtime().tm_mon
    4

    1.2转换

    time.localtime() 可以传入参数, time.time()不行

    >>> tt=time.time()
    >>> tl=time.localtime()
    >>> 
    >>> time.localtime(tt)
    time.struct_time(tm_year=2018, tm_mon=4, tm_mday=15, tm_hour=17, tm_min=26, tm_sec=42, tm_wday=6, tm_yday=105, tm_isdst=0)
    >>> time.time(tl)
    Traceback (most recent call last):
      File "<pyshell#84>", line 1, in <module>
        time.time(tl)
    TypeError: time() takes no arguments (1 given)
    >>>

    >>> time.mktime(tl)
    1523784410.0

    1.3格式化

    可以对time.localtime()格式化,不能对time.time()格式化

    >>> "%s-%s-%s %s:%s:%s"%(tl.tm_year,tl.tm_mon,tl.tm_mday,tl.tm_hour,tl.tm_min,tl.tm_sec)
    >>> time.strftime('%Y-%m-%d %H:%M:%S',tl)
    '2018-04-15 17:26:50'
    >>> 
    >>> time.strftime('%Y-%m-%d %H:%M:%S',tt)
    Traceback (most recent call last):
      File "<pyshell#105>", line 1, in <module>
        time.strftime('%Y-%m-%d %H:%M:%S',tt)
    TypeError: Tuple or struct_time argument required
    >>> 

    或者直接对当前时间格式化

    >>> time.strftime('%Y-%m-%d %H:%M:%S')
    '2018-04-15 18:21:42'
    >>> 

    1.4 格式化与反格式化

    >>> s1=time.strftime('%Y-%m-%d %H:%M:%S')
    >>> time.strptime(s1,'%Y-%m-%d %H:%M:%S')
    time.struct_time(tm_year=2018, tm_mon=4, tm_mday=15, tm_hour=18, tm_min=37, tm_sec=11, tm_wday=6, tm_yday=105, tm_isdst=-1)
    >>> 

     1.5 全程

    >>> tt=time.time()
    >>> tt
    1523789896.6532319
    >>> tl=time.localtime(tt)
    >>> time.strftime('%Y-%m-%d',tl)   #丢失了 小时 分钟 等信息
    '2018-04-15'
    >>> time.strptime('2018-04-15','%Y-%m-%d')
    time.struct_time(tm_year=2018, tm_mon=4, tm_mday=15, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=105, tm_isdst=-1)
    >>> tl2=time.strptime('2018-04-15','%Y-%m-%d')
    >>> time.mktime(tl2)
    1523721600.0
    >>> 

    1.6 帮助

    >>> help(time.strftime)
    Help on built-in function strftime in module time:

    strftime(...)
    strftime(format[, tuple]) -> string

    Convert a time tuple to a string according to a format specification.
    See the library reference manual for formatting codes. When the time tuple
    is not present, current time as returned by localtime() is used.

    Commonly used format codes:

    %Y Year with century as a decimal number.
    %m Month as a decimal number [01,12].
    %d Day of the month as a decimal number [01,31].
    %H Hour (24-hour clock) as a decimal number [00,23].
    %M Minute as a decimal number [00,59].
    %S Second as a decimal number [00,61].
    %z Time zone offset from UTC.
    %a Locale's abbreviated weekday name.
    %A Locale's full weekday name.
    %b Locale's abbreviated month name.
    %B Locale's full month name.
    %c Locale's appropriate date and time representation.
    %I Hour (12-hour clock) as a decimal number [01,12].
    %p Locale's equivalent of either AM or PM.

    Other codes may be available on your platform. See documentation for
    the C library strftime function.

    >>>

    2 datetime模块

    2.1测试

    >>> 
    >>> ddn=datetime.datetime.now()
    >>> ddn
    datetime.datetime(2018, 4, 15, 19, 3, 47, 798172)
    >>> ddn.year
    2018
    >>> 

     2.2 转换

    time.time 转换为datetime对象格式

    >>> datetime.datetime.fromtimestamp(time.time())
    datetime.datetime(2018, 4, 15, 19, 9, 53, 623096)

    2.3 格式化

    >>> ddn=datetime.datetime.now()
    >>> ddn.strftime('%Y-%m-%d')
    '2018-04-15'
    >>> datetime.datetime.strftime(ddn,'%Y-%m-%d')
    '2018-04-15'

    2.4 日期相差

    >>> datetime.datetime.now()- datetime.timedelta(hours=3)
    datetime.datetime(2018, 4, 15, 16, 18, 19, 118009)
    >>> 
    >>> datetime.datetime.now()- datetime.timedelta(days=1)
    datetime.datetime(2018, 4, 14, 19, 18, 24, 542319)
    >>> 

     >>> datetime.datetime.now()- datetime.timedelta(minutes=30)
     datetime.datetime(2018, 4, 15, 18, 49, 5, 502662)
     >>>

     日期改写

    >>> ddn=datetime.datetime.now()
    >>> ddn
    datetime.datetime(2018, 4, 15, 19, 20, 20, 565955)
    >>> ddn.replace(year=2016)
    datetime.datetime(2016, 4, 15, 19, 20, 20, 565955)
    >>> 
  • 相关阅读:
    Menu-actionBarMenu字体颜色修改
    actionBarTab-actionBarTab自定义 布局没法改变其中字体相对中间的位置
    Funui-overlay 如何添加theme 的 overlay
    java进阶——反射(Reflect)
    java工具类学习整理——集合
    Java实例练习——java实现自动生成长度为10以内的随机字符串(可用于生成随机密码)
    打通Java与MySQL的桥梁——jdbc
    SQL数据库操作整理
    PhpStorm 4.0 & 5.0 部署本地Web应用
    PhpStorm 4.0 & 5.0 部署本地Web应用
  • 原文地址:https://www.cnblogs.com/infaaf/p/8847863.html
Copyright © 2020-2023  润新知