python library python datetime Date and time of addition and subtraction

python datetime library and add or subtract computing

 From: https: //www.cnblogs.com/linkenpark/p/8079337.html

datetime library

First, the operating current time

1. Get the current time

>>> import datetime
>>> print datetime.datetime.now()
2019-07-11 14:24:01.954000

Time format the output:

Copy the code
>>> print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
2019-07-11 14:25:33
>>> print datetime.datetime.now().strftime("%Y%m%d")
20190711
>>> print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
2019-07-11 14:25
Copy the code

Use timedelta method of the present time add or subtract

Plus one minute

>>> print (datetime.datetime.now()+datetime.timedelta(minutes=1)).strftime("%Y-%m-%d %H:%M:%S")
2019-07-11 14:29:46

Minus one minute

>>> print (datetime.datetime.now()+datetime.timedelta(minutes=-1)).strftime("%Y-%m-%d %H:%M:%S")
2019-07-11 14:29:32

Plus one day

>>> print (datetime.datetime.now()+datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S")
2019-07-12 14:32:37

Plus one hour

>>> print (datetime.datetime.now()+datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S")
2019-07-11 15:33:37

You may also be used timedelta method specified time for addition and subtraction: first specified time to process

Copy the code
strTime = '2019-07-11 11:03' # a given time, this is a string 
startTime = datetime.datetime.strptime (strTime, "% Y-% m-% d% H:% M") # the strTime converted to time format, auto fill position behind the second bit 
Print the startTime 
Print startTime.strftime ( "% D%% Y-M-% H:% M") # formatted output, and to maintain a given format consistent 
# startTime time plus one minute 
startTime2 = (the datetime.timedelta the startTime + (= 2 minutes)) the strftime. ( "% D%% Y-M-% H:% M") 
Print startTime2
Copy the code

Output:

2019-07-11 11:03:00
2019-07-11 11:03
2019-07-11 11:05

Process finished with exit code 0

 Plus cycle time

Copy the code
startTime = '2019-07-11 23:30:00' # enter a time, this is string 
# endTime = '2019-07-11 15:35' 
for I in Range (. 3): 
    endTime = (A datetime.datetime .strptime (the startTime, "% Y-M-% D%% H:% M:% S") + the datetime.timedelta ( 
        . = Days. 1)) the strftime ( "% D%% Y-M-% H:% M:% S ") 
    Print the startTime, endTime 
    the startTime = endTime 

# parameters days = 1 (day + 1) can be replaced minutes = 1 (min +1), seconds = 1 (s + 1)
Copy the code
Output:
2019-07-11 23:30:00 2019-07-12 23:30:00
2019-07-12 23:30:00 2019-07-13 23:30:00
2019-07-13 23:30:00 2019-07-14 23:30:00

Process finished with exit code 0

 

datetime library

First, the operating current time

1. Get the current time

>>> import datetime
>>> print datetime.datetime.now()
2019-07-11 14:24:01.954000

Time format the output:

Copy the code
>>> print datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
2019-07-11 14:25:33
>>> print datetime.datetime.now().strftime("%Y%m%d")
20190711
>>> print datetime.datetime.now().strftime("%Y-%m-%d %H:%M")
2019-07-11 14:25
Copy the code

Use timedelta method of the present time add or subtract

Plus one minute

>>> print (datetime.datetime.now()+datetime.timedelta(minutes=1)).strftime("%Y-%m-%d %H:%M:%S")
2019-07-11 14:29:46

Minus one minute

>>> print (datetime.datetime.now()+datetime.timedelta(minutes=-1)).strftime("%Y-%m-%d %H:%M:%S")
2019-07-11 14:29:32

Plus one day

>>> print (datetime.datetime.now()+datetime.timedelta(days=1)).strftime("%Y-%m-%d %H:%M:%S")
2019-07-12 14:32:37

Plus one hour

>>> print (datetime.datetime.now()+datetime.timedelta(hours=1)).strftime("%Y-%m-%d %H:%M:%S")
2019-07-11 15:33:37

You may also be used timedelta method specified time for addition and subtraction: first specified time to process

Copy the code
strTime = '2019-07-11 11:03' # a given time, this is a string 
startTime = datetime.datetime.strptime (strTime, "% Y-% m-% d% H:% M") # the strTime converted to time format, auto fill position behind the second bit 
Print the startTime 
Print startTime.strftime ( "% D%% Y-M-% H:% M") # formatted output, and to maintain a given format consistent 
# startTime time plus one minute 
startTime2 = (the datetime.timedelta the startTime + (= 2 minutes)) the strftime. ( "% D%% Y-M-% H:% M") 
Print startTime2
Copy the code

Output:

2019-07-11 11:03:00
2019-07-11 11:03
2019-07-11 11:05

Process finished with exit code 0

 Plus cycle time

Copy the code
startTime = '2019-07-11 23:30:00' # enter a time, this is string 
# endTime = '2019-07-11 15:35' 
for I in Range (. 3): 
    endTime = (A datetime.datetime .strptime (the startTime, "% Y-M-% D%% H:% M:% S") + the datetime.timedelta ( 
        . = Days. 1)) the strftime ( "% D%% Y-M-% H:% M:% S ") 
    Print the startTime, endTime 
    the startTime = endTime 

# parameters days = 1 (day + 1) can be replaced minutes = 1 (min +1), seconds = 1 (s + 1)
Copy the code
Output:
2019-07-11 23:30:00 2019-07-12 23:30:00
2019-07-12 23:30:00 2019-07-13 23:30:00
2019-07-13 23:30:00 2019-07-14 23:30:00

Process finished with exit code 0

 

Guess you like

Origin www.cnblogs.com/gisoracle/p/11822881.html