zoukankan      html  css  js  c++  java
  • python datetime获取几分钟、小时、天之前的时间

    import datetime
    
    
    print ((datetime.datetime.now()-datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M"))
    print ((datetime.datetime.now()-datetime.timedelta(minutes=1)).strftime("%Y-%m-%d %H:%M"))
    print ((datetime.datetime.now()-datetime.timedelta(seconds=1)).strftime("%Y-%m-%d %H:%M"))
    

      输出结果

     以下是随机获取15天前的日期和时间实例:

    import datetime
    import random
    d = random.randint(0,15)
    date = ((datetime.datetime.now()-datetime.timedelta(days=d)).strftime("%Y-%m-%d %H:%M:%S"))
    # print date
    day = date[0:11]
    # print "day:", day
    
    h = random.randint(0,24)
    date = ((datetime.datetime.now()-datetime.timedelta(hours=h)).strftime("%Y-%m-%d %H:%M:%S"))
    # print date
    hour = date[11:13]
    # print "hour: ",hour
    
    m = random.randint(0,15)
    date = ((datetime.datetime.now()-datetime.timedelta(minutes=m)).strftime("%Y-%m-%d %H:%M:%S"))
    # print date
    minue = date[14:16]
    # print "minue: ", minue
    s = random.randint(0,24)
    date = ((datetime.datetime.now()-datetime.timedelta(seconds=s)).strftime("%Y-%m-%d %H:%M:%S"))
    # print date
    second = date[17:19]
    # print "second: ", second
    
    new_time = hour+":"+minue+":"+second
    print day,new_time
    

     输出结果:

     

    实例:

    #coding:UTF-8
    
    import time 
    import datetime
    import random
    
    def get_time():
        d = random.randint(0,15)
        date = ((datetime.datetime.now()-datetime.timedelta(days=d)).strftime("%Y-%m-%d %H:%M:%S"))
        # print date
        day = date[0:11]
        # print "day:", day
         
        h = random.randint(0,24)
        date = ((datetime.datetime.now()-datetime.timedelta(hours=h)).strftime("%Y-%m-%d %H:%M:%S"))
        # print date
        hour = date[11:13]
        # print "hour: ",hour
         
        m = random.randint(0,15)
        date = ((datetime.datetime.now()-datetime.timedelta(minutes=m)).strftime("%Y-%m-%d %H:%M:%S"))
        # print date
        minue = date[14:16]
        # print "minue: ", minue
        s = random.randint(0,24)
        date = ((datetime.datetime.now()-datetime.timedelta(seconds=s)).strftime("%Y-%m-%d %H:%M:%S"))
        # print date
        second = date[17:19]
        # print "second: ", second
         
        new_time = hour+":"+minue+":"+second
        return day.strip(),new_time.strip()
    
    day,times=get_time()
    print day
    print times
    str_1 = '{"PN":"34VT123","DUID":"XXXX","location":{"coordinates":[-100.35256443,33.4586858]},"SPD":125,"DT":"'
    str_2 = 'Z","driverID":"XXXXX","cate":"event","subCate":"OBDII","eventOBDII":{"what":"emergencyBrake","param":2,"GID":123456479}}'
    des_str = '2017-06-02T13:15:20'
    str_des = str_1 + day + 'T' + times + str_2
    print str_des
    

     输出

    > "D:Python27python.exe"  "F:MQTT_testingdata.py" 
    2017-05-26
    02:21:19
    {"PN":"34VT123","DUID":"XXXX","location":{"coordinates":[-100.35256443,33.4586858]},"SPD":125,"DT":"2017-05-26T02:21:19Z","driverID":"XXXXX","cate":"event","subCate":"OBDII","eventOBDII":{"what":"emergencyBrake","param":2,"GID":123456479}}
    

      

     

  • 相关阅读:
    nginx配置文件中的location中文详解
    binlog、redo log、undo log区别
    OLTP和OLAP的区别
    MPP、SMP、NUMA概念介绍
    NUMA体系结构介绍
    在Linux下判断系统当前是否开启了超线程
    NUMA的取舍与优化设置
    LRU缓存算法
    optimize table
    使用innodb_force_recovery解决MySQL崩溃无法重启问题
  • 原文地址:https://www.cnblogs.com/saryli/p/6934913.html
Copyright © 2011-2022 走看看