zoukankan      html  css  js  c++  java
  • time & datetime

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

    • 时间戳               1970年1月1日之后的秒,即:time.time()
    • 格式化的字符串    2014-11-11 11:11,    即:time.strftime('%Y-%m-%d')
    • 结构化时间          元组包含了:年、日、星期等... time.struct_time    即:time.localtime()
    print(time.gmtime())    #可加时间戳参数,将一个时间戳转换为UTC时区(0时区)的struct_time.其默认值为time.time(),函数返回time.struct_time类型的对象。(struct_time是在time模块中定义的表示时间的对象)
    print(time.localtime()) #可加时间戳参数,作用是格式化时间戳为本地的时间
    print(time.strptime('2014-11-11', '%Y-%m-%d')) #字符串转换成time.struct格式
    例:
    print(time.strptime('2016-7-18','%Y-%m-%d'))

      time.struct_time(tm_year=2016, tm_mon=7, tm_mday=18, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=200, tm_isdst=-1)

       
    print(time.strftime('%Y-%m-%d')) #默认当前时间
    print(time.strftime('%Y-%m-%d',time.localtime())) #默认当前时间
    print(time.asctime())   #返回一个可读的形式为"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18时07分14秒)的24个字符的字符串。
    print(time.asctime(time.localtime()))
    print(time.ctime(time.time()))  #把一个时间戳(按秒计算的浮点数)转化为time.asctime()的形式
       
    from datetime import date,datetime 
    
    '''
    datetime.date:表示日期的类。常用的属性有year, month, day
    datetime.time:表示时间的类。常用的属性有hour, minute, second, microsecond
    datetime.datetime:表示日期时间
    datetime.timedelta:表示时间间隔,即两个时间点之间的长度,直属datetime类
    timedelta([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, weeks]]]]]]])
    strftime("%Y-%m-%d")
    '''
    
    print(datetime.now())  #返回新的时间对象
    	例:
      In [10]: datetime.now()
      Out[10]: datetime.datetime(2016, 6, 22, 9, 43, 4, 625646)
    
    print(date.weekday(datetime.now()))  #星期几 星期一 到 星期六 对应 0-6
    	例:
    	In [13]: date.weekday(datetime.now())
    	Out[13]: 2
    
    print(datime.today().day) #今天几号
    print(datetime.datetime.now() - datetime.timedelta(days=5)) #时间减法
      
    (datetime.datetime.now()-datetime.timedelta(days=30)).strftime("%Y-%m-%d") #时间减法 strftime 为可读字串
      例:

        In [43]: (datetime.datetime.now()-datetime.timedelta(days=30)).strftime("%Y-%m-%d")
        Out[43]: '2016-05-23'

      

        %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.
  • 相关阅读:
    软件定义网络实验4:Open vSwitch 实验——Mininet 中使用 OVS 命令(实验过程及结果记录)
    软件定义网络实验3:测量路径的损耗率 (实验过程及结果记录)
    第一次个人编程作业
    软件定义网络实验2:Mininet拓扑的命令脚本生成(实验过程及结果记录)
    软件定义网络实验1:Mininet源码安装和可视化拓扑工具(实验过程及结果记录)
    第一次博客作业
    第07组(69) 需求分析报告
    第七组(69)团队展示
    第三次作业
    结对编程作业
  • 原文地址:https://www.cnblogs.com/yangxiaolan/p/5507833.html
Copyright © 2011-2022 走看看