zoukankan      html  css  js  c++  java
  • Python学习记录 时间

    note:以下类容来源于网络,作为自己学习摘抄记录,方便以后查看

     

    Python提供time时间模块需要单独引入

    #推迟调用线程的运行,secs指秒数。
    time.sleep(secs)

     

    时间戳

    时间戳都以自从1970年1月1日午夜经过了多长时间来表示,时间间隔是以秒为单位的浮点小数。

    import time   # 引入time模块
    print U"当前时间戳:",time.time()

    输出结果:

    当前时间戳: 1471487935.02

    当前时间 

    time.localtime() 函数 用一个元组装起来的9组数字处理时间:

    9组数据分别是:tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst

    import time
    print time.localtime(time.time())
    print time.localtime()

    输出结果:

    time.struct_time(tm_year=2016, tm_mon=8, tm_mday=18, tm_hour=14, tm_min=16, tm_sec=16, tm_wday=3, tm_yday=231, tm_isdst=0)
    time.struct_time(tm_year=2016, tm_mon=8, tm_mday=18, tm_hour=14, tm_min=16, tm_sec=16, tm_wday=3, tm_yday=231, tm_isdst=0)

     格式化时间

    获取可读的时间模式的函数是asctime()

    print time.asctime( time.localtime() )

    输出结果:

    Thu Aug 18 14:04:03 2016

    按需求格式化

    import time
    
    print time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())
    print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime()) 
    print time.strftime("%Y%m%d",time.localtime())
    # 将格式字符串转换为时间戳
    a = "Thu Aug 18 14:07:27 2016"
    print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))

    输出结果:

    2016-08-18 14:08:13
    Thu Aug 18 14:08:13 2016
    20160818
    1471500447.0

    python中时间日期格式化符号:

        %y 两位数的年份表示(00-99%Y 四位数的年份表示(000-9999%m 月份(01-12%d 月内中的一天(0-31%H 24小时制小时数(0-23%I 12小时制小时数(01-12%M 分钟数(00=59%S 秒(00-59%a 本地简化星期名称
        %A 本地完整星期名称
        %b 本地简化的月份名称
        %B 本地完整的月份名称
        %c 本地相应的日期表示和时间表示
        %j 年内的一天(001-366%p 本地A.M.或P.M.的等价符
        %U 一年中的星期数(00-53)星期天为星期的开始
        %w 星期(0-6),星期天为星期的开始
        %W 一年中的星期数(00-53)星期一为星期的开始
        %x 本地相应的日期表示
        %X 本地相应的时间表示
        %Z 当前时区的名称
        %% %号本身
  • 相关阅读:
    LeetCode 326. Power of Three
    LeetCode 324. Wiggle Sort II
    LeetCode 322. Coin Change
    LeetCode 321. Create Maximum Number
    LeetCode 319. Bulb Switcher
    LeetCode 318. Maximum Product of Word Lengths
    LeetCode 310. Minimum Height Trees (DFS)
    个人站点大开发!--起始篇
    LeetCode 313. Super Ugly Number
    LeetCode 309. Best Time to Buy and Sell Stock with Cooldown (DP)
  • 原文地址:https://www.cnblogs.com/llgg/p/5783152.html
Copyright © 2011-2022 走看看