复杂时间的处理分钟、小时、前等

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ZHH_Love123/article/details/84334212

python datetime获取几分钟、小时、天之前的时间

1

2

3

4

5

6

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"))

时间处理 

datetime.datetime.now().month 获取当前月份 (year,month,day)

temp = response.doc("div.scd-title em").text()
                if temp.find("今天") !=-1:
                    temp = "%s%s%s%s%s" %(str(datetime.datetime.now().month),"-",str(datetime.datetime.now().day)," ",temp.replace("今天",""))
                else:
                    temp = temp.replace("月","-").replace("日","")
                pub_time ="%s%s%s" %(str(datetime.datetime.now().year),"-",temp)
            print("pub_time:"+pub_time)

  输出结果

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

import datetime

import random

= 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

= 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

= 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

= 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

 输出结果:

 

实例:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

#coding:UTF-8

import time

import datetime

import random

def get_time():

    = 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

      

    = 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

      

    = 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

    = 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

 输出

1

2

3

4

"D:\Python27\python.exe"  "F:\MQTT_testing\data.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}}

处理a = "20181107" 可以用切片 拼接 a_time = '-'.join([a[0:4],a[4:6],a[6:8]])
  

猜你喜欢

转载自blog.csdn.net/ZHH_Love123/article/details/84334212