zoukankan      html  css  js  c++  java
  • 二十三、python中的time和datetime模块

    A、time模块

      1. sleep():强制等待

    import time
    import datetime

    print("start to sleep.....")
    time.sleep(5)
    print("wake up ...........")
    '''
    2.time.time():当前系统的时间戳
    '''
    print(time.time())
    ---------------------------
    1533021214.0050707
    ---------------------------
    '''
    3.time.ctime():当前系统时间
    '''
    print (time.ctime())
    print(time.ctime(time.time()-86400))#昨天的此刻
    ---------------------------
    Tue Jul 31 15:13:34 2018
    Mon Jul 30 15:13:34 2018
    ---------------------------
    '''
    4.time.gmtime():将时间转化成struct_time格式
    '''
    print (time.gmtime())
    time_obj=time.gmtime(time.time()-86400) #昨天的此刻(将时间戳转化成struct_time格式
    print (time_obj)
    print (str(time_obj.tm_year)+"-"+str(time_obj.tm_mon)+"-"+str(time_obj.tm_mday))
    print("%s-%s-%s %s:%s:%s" %(time_obj.tm_year,time_obj.tm_mon,time_obj.tm_mday,time_obj.tm_hour,time_obj.tm_min,time_obj.tm_sec))
    ---------------------------

        time.struct_time(tm_year=2018, tm_mon=7, tm_mday=31, tm_hour=7, tm_min=32, tm_sec=9, tm_wday=1, tm_yday=212, tm_isdst=0)
     time.struct_time(tm_year=2018, tm_mon=7, tm_mday=30, tm_hour=7, tm_min=32, tm_sec=9, tm_wday=0, tm_yday=211, tm_isdst=0)
     2018-7-30
     2018-7-30 7:32:9

    ---------------------------
    '''
    4.1.字符串格式化(扩展)
    %s:字符串
    %d:整型
    %f:浮点型
    '''
    time_obj=time.gmtime(time.time()-86400)  #昨天的此刻
    print("%s-%s-%s %s:%s:%s" %(time_obj.tm_year,time_obj.tm_mon,time_obj.tm_mday,time_obj.tm_hour,time_obj.tm_min,time_obj.tm_sec))
    ---------------------------
    2018-7-30 7:32:9
    ---------------------------
    '''
    5.localtime():将时间戳转化成struct_time格式,返回本地的时间
    '''
    print (time.localtime(time.time()-86400))
    ---------------------------
    time.struct_time(tm_year=2018, tm_mon=7, tm_mday=31, tm_hour=14, tm_min=51, tm_sec=20, tm_wday=1, tm_yday=212, tm_isdst=0)
    ---------------------------
    '''
    6.time.mktime(time.localtime()):将struct_time转化成时间戳格式
    '''
    print (time.mktime(time.localtime()))
    ---------------------------
    1533106558.0
    ---------------------------
    '''
    7.time.strptime():将字符串格式日期转化成struct_time格式
    '''

    print (time.strptime("2018-08-01 15:02","%Y-%m-%d %H:%M"))
    ---------------------------

     time.struct_time(tm_year=2018, tm_mon=8, tm_mday=1, tm_hour=15, tm_min=2, tm_sec=0, tm_wday=2, tm_yday=213, tm_isdst=-1)

    ---------------------------
    '''
    8.time.strftime():将时间对象转成指定的字符串格式
    '''
    print (time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime()))
    print (time.strftime("%Y-%m-%d %H:%M:%S",time.localtime()))
    ---------------------------

       2018-08-01 07:05:10
       2018-08-01 15:05:10

    ---------------------------
    '''
    9.time.asctime():返回时间格式"Fri Aug 19 11:14:16 2016
    '''
    print(time.asctime())
    ---------------------------

       Wed Aug  1 15:06:53 2018

    ---------------------------
    B、datatime
    '''
    1.datetime.date.today():打印当天的日期,格式:2018-08-01
    '''
    print (datetime.date.today())
    ---------------------------

       2018-08-01

    ---------------------------
    '''
    2.datetime.date.fromtimestamp(time.time()):将时间戳转化成日期格式
    '''
    print (datetime.date.fromtimestamp(time.time()))
    ---------------------------

       2018-08-01

    ---------------------------
    '''
    3.datetime.datetime.now():返回当前时间,时分秒
    datetime.datetime.now().timetuple():将当前时间转化成struct_time格式
    '''
    current_time=datetime.datetime.now()
    print(current_time)
    print(current_time.timetuple())
    --------------------------- 

        2018-08-01 15:20:18.638428
        time.struct_time(tm_year=2018, tm_mon=8, tm_mday=1, tm_hour=15, tm_min=20, tm_sec=18, tm_wday=2, tm_yday=213, tm_isdst=-1)

    ---------------------------
    '''
    4.datetime.datetime.now().replace(yyyy,mm,dd):将当前时间替换成指定时间
      datetime.datetime.now().replace():指定格式为空的话返回当前日期

       ' '
      current_time=datetime.datetime.now()
      print(current_time.replace())
      print(current_time.replace(2066,12,12))

    
    
    ---------------------------  

        2018-08-01 15:27:29.508072
        2066-12-12 15:27:29.508072

    ---------------------------  
    '''
    5.时间的加减
    '''
    print (datetime.datetime.now()+datetime.timedelta(days=10)) #比现在加10天
    print (datetime.datetime.now()-datetime.timedelta(days=10)) #比现在减10天
    print (datetime.datetime.now()+datetime.timedelta(hours=2)) #比现在加10小时
    print (datetime.datetime.now()-datetime.timedelta(seconds=10)) #比现在减10秒
    print (datetime.datetime.now()-datetime.timedelta(minutes=10)) #比现在减10分钟
    print (datetime.datetime.now()+datetime.timedelta(weeks=1)) #比现在加一周
    ---------------------------  

      2018-08-11 15:56:19.873044
      2018-07-22 15:56:19.873044
      2018-08-01 17:56:19.873044
      2018-08-01 15:56:09.873044
      2018-08-01 15:46:19.873044
      2018-08-08 15:56:19.873044

    ---------------------------
     
     
  • 相关阅读:
    闭包
    递归 斐波那契
    作用域,函数的四种调用模式
    数组方法
    Math内置对象
    BeanUtils.copyProperties() 用法
    ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) .append("Id",getId())防止内存泄漏
    Spring配置文件applicationContext.xml Hibernate SQL方言 (hibernate.dialect)
    Redis面试总结
    Nginx面试题
  • 原文地址:https://www.cnblogs.com/chushujin/p/9396129.html
Copyright © 2011-2022 走看看