zoukankan      html  css  js  c++  java
  • time模块,datetime模块

    time模块

    time模块是包含各方面对时间操作的函数. 尽管这些常常有效但不是所有方法在任意平台中有效。

    时间相关的操作,时间有三种表示方式:

    • 时间戳               1970年1月1日之后的秒,即:time.time()
    • 格式化的字符串    2014-11-11 11:11,    即:time.strftime('%Y-%m-%d')
    • 结构化时间          元组包含了:年、日、星期等... time.struct_time    即:time.localtime()
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    import time
     
    # ========== 时间戳 ==========
    print time.time()  
    print time.mktime(time.localtime())
        
    # ========== 元组形式 ==========
    print time.gmtime()    #可加时间戳参数
    print time.localtime() #可加时间戳参数
    print time.strptime('2014-11-11''%Y-%m-%d')
        
    # ========== 字符串形式 ==========
    print time.strftime('%Y-%m-%d'#默认当前时间
    print time.strftime('%Y-%m-%d',time.localtime()) #默认当前时间
    print time.asctime()
    print time.asctime(time.localtime())
    print time.ctime(time.time())
     
    """
    格式化占位符:
     
        %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.
     
    """

    datetime模块

    datetime模块提供对于日期和时间进行简单或复杂的操作. datetime 模块提供了一下的可用类型(Available Types)。

    • datetime.MINYEAR 和 datetime.MAXYEAR 模块常量表示datetime接受的范围
    • class datetime.date: 一个理想化的日期, 提供year, month, day属性
    • class datetime.time: 一个理想化的时间, 提供hour, minute, second, microsecond, tzinfo.
    • class datetime.datetime: 日期和时间的组合.提供year, month, day, hour, minute, second, microsecond, tzinfo.
    • class datetime.timedelta: 表达两个date,time和datetime持续时间内的微妙差异.
    • class datetime.tzinfo: 时间对象的抽象基类.
    1
    2
    3
    4
    # 时间相减
    import datetime
    print datetime.datetime.now()
    print datetime.datetime.now() - datetime.timedelta(days=5)
  • 相关阅读:
    我的前端故事----欢乐大富翁( ̄︶ ̄)↗ (摇骰子+棋盘)
    我的前端故事----优美的编辑器GitHub Atom
    我的前端故事----Ajax方式和jsonp的实现区别
    关于Web服务器的认识
    Django--全文检索功能
    对Ajax连接的认识~为毛不能上传文件!!!
    前端页面--天猫右侧信息栏弹出效果实现
    前端页面--瀑布流布局的实现
    Django分析之如何自定义manage命令
    Django分析之使用redis缓存服务器
  • 原文地址:https://www.cnblogs.com/my-blogs-for-everone/p/8890948.html
Copyright © 2011-2022 走看看