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

    #!/usr/bin/env python
    # -*- coding:utf-8 -*-
    # Author:Random_lee
    import time
    a=time.time()
    # 时间戳
    print(a)
    a=time.localtime()
    # 本地时间元组的形式
    print(a)
    a=time.timezone
    # 时区28800/3600=8 东八区
    print(a)
    a=time.altzone
    # 夏令时与utc时间之间的差值
    print(a)
    a=time.daylight
    # 是否使用了夏令时
    # 0就是没有使用
    print(a)


    a=time.gmtime(1522490733.7783701)
    # 将时间戳转换为struct_time(结构时间) 标准utc时间
    print(a)
    a=time.localtime(1522490733.7783701)
    # 将时间戳转换为struct_time(结构时间) 本地utc时区的时间
    print(a)
    # print(help(a))
    print(a.tm_year,'年',a.tm_mon,'月')
    # 获取结构时间的值


    b=time.mktime(a)
    # 结构化时间转换为时间戳
    print(b)


    b=time.strftime('%Y-%m-%d %H:%M:%S %z %a %A %b %B %c %I %p')
    # 结构化时间转换为格式化时间
    #ctrl+b或者help(time.strftime)查看函数的使用方法 默认不加时间元组参数的时候取的值是time.localtime
    # print(help(time.strftime))
    b1=time.strftime('%Y-%m-%d %H:%M:%S %z %a %A %b %B %c %I %p',a)
    print(b)
    print(b1)

    b2=time.strptime('2018-04-01 12:23:30','%Y-%m-%d %H:%M:%S')
    # 格式化时间转换为结构化时间
    print(b2)


    b2=time.asctime(a)
    b3=time.asctime()
    # 将结构化时间转换为格式化时间
    # 默认转递time.localtime()
    print(a)
    print(type(a))
    print(b2)
    print(b3)

    b4=time.ctime()
    b5=time.ctime(987656764)
    # 将时间戳转换为格式化时间
    # 默认转换当前时间的时间戳
    print(help(time.ctime))
    print(b4)
    print(b5)

    import datetime
    c=datetime.datetime.now()
    # 获取当前时间
    c1=datetime.datetime.now()+datetime.timedelta(3)
    # 当前时间的后三天
    c2=datetime.datetime.now()+datetime.timedelta(-3)
    # 当前时间的前三天
    c3=datetime.datetime.now()+datetime.timedelta(hours=3)
    # 当前时间的后三小时
    c4=datetime.datetime.now()+datetime.timedelta(hours=-3)
    # 当前时间的前三小时
    c5=datetime.datetime.now()+datetime.timedelta(minutes=10)
    # 当前时间的后十分钟
    c6=datetime.datetime.now()+datetime.timedelta(minutes=-10)
    # 当前时间的前十分钟
    c7=datetime.datetime.now()
    c8=c7.replace(minute=3,hour=2)
    # 时间替换 修改获取到的当前时间

    print(c,'获取当前时间')
    print(c1,'当前时间的后三天')
    print(c2,'当前时间的前三天')
    print(c3,'当前时间的后三小时')
    print(c4,'当前时间的前三小时')
    print(c5,'当前时间的后十分钟')
    print(c6,'当前时间的前十分钟')
    print(c8,'时间替换',sep='')



     
    DirectiveMeaningNotes
    %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.  
    %d Day of the month as a decimal number [01,31].  
    %H Hour (24-hour clock) as a decimal number [00,23].  
    %I Hour (12-hour clock) as a decimal number [01,12].  
    %j Day of the year as a decimal number [001,366].  
    %m Month as a decimal number [01,12].  
    %M Minute as a decimal number [00,59].  
    %p Locale’s equivalent of either AM or PM. (1)
    %S Second as a decimal number [00,61]. (2)
    %U Week number of the year (Sunday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Sunday are considered to be in week 0. (3)
    %w Weekday as a decimal number [0(Sunday),6].  
    %W Week number of the year (Monday as the first day of the week) as a decimal number [00,53]. All days in a new year preceding the first Monday are considered to be in week 0. (3)
    %x Locale’s appropriate date representation.  
    %X Locale’s appropriate time representation.  
    %y Year without century as a decimal number [00,99].  
    %Y Year with century as a decimal number.  
    %z Time zone offset indicating a positive or negative time difference from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal hour digits and M represents decimal minute digits [-23:59, +23:59].  
    %Z Time zone name (no characters if no time zone exists).  
    %% A literal '%' character.

    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 当前时区的名称
    • %% %号本身
  • 相关阅读:
    Android 压力测试工具Monkey
    解决maven的依赖总是无法下载完成
    JDBC连接数据库(二)
    JDBC连接数据库(一)
    webdriver js点击无法点击的元素
    多线程Java面试题总结
    PHP unset销毁变量并释放内存
    ThinkPHP函数详解:D方法
    PHP 函数:intval()
    ThinkPHP 模板显示display和assign的用法
  • 原文地址:https://www.cnblogs.com/randomlee/p/8794970.html
Copyright © 2011-2022 走看看