(Reprinted) python date function, reprinted python date function
Reprinted on http://www.cnblogs.com/emanlee/p/4399147.html
All Date and Time APIs are in the datetime module.
1. format the date output datetime => string
Import datetime
Now = datetime. datetime. now ()
Now. strftime ('% Y-% m-% d % H: % M: % s ')
Output
'2017-04-07 19:11:21'
Strftime is an instance method of the datetime class.
2. format the date output string => datetime
Import datetime
T_str = '2017-04-07 19:11:21'
D = datetime. datetime. strptime (t_str, '% Y-% m-% d % H: % M: % s ')
Strptime is a static method of the datetime class.
3. Date comparison operation
In the datetime module, there is a timedelta class. The objects of this class are used to represent a time interval, for example, the difference between two dates or times.
Constructor:
Import datetime
Datetime. timedelta (days = 0, seconds = 0, microseconds = 0, milliseconds = 0, minutes = 0, hours = 0, weeks = 0)
All parameters have the default value 0. These parameters can be int, float, positive, or negative.
You can use timedelta. days and tiemdelta. seconds to obtain the corresponding time value.
Instances of the timedelta class support addition, subtraction, multiplication, division, and other operations. The result is also an instance of the timedelta class. For example:
Import datetime
Year = datetime. timedelta (days = 365)
Ten_years = year * 10
Nine_years = ten_years-year
At the same time, the date, time, and datetime classes also support addition and subtraction operations with timedelta.
Datetime1 = datetime2 + timedelta
Timedelta = datetime1-datetime2
In this way, you can easily implement some functions.
4. How many days do the two dates differ.
Import datetime
D1 = datetime. datetime. strptime ('2017-03-05 17:41:20 ',' % Y-% m-% d % H: % M: % s ')
D2 = datetime. datetime. strptime ('2017-03-02 17:41:20 ',' % Y-% m-% d % H: % M: % s ')
Delta = d1-d2
Print delta. days
Output: 3
5. The date of n days after today.
Import datetime
Now = datetime. datetime. now ()
Delta = datetime. timedelta (days = 3)
N_days = now + delta
Print n_days.strftime ('% Y-% m-% d % H: % M: % s ')
Output:
19:16:34
# Coding = UTF-8
Import datetime
Now = datetime. datetime. now ()
Print now
# Convert a date to a string
Datetime => string
Import datetime
Now = datetime. datetime. now ()
Print now. strftime ('% Y-% m-% d % H: % M: % s ')
# Convert a string to a dateString => datetime
Import datetime
T_str = '2017-03-05 16:26:23'
D = datetime. datetime. strptime (t_str, '% Y-% m-% d % H: % M: % s ')
Print d
# There is a timedelta class in the datetime module. The objects of this class are used to represent a time interval, for example, the difference between two days # or time.
# Calculate the interval between two dates
Import datetime
D1 = datetime. datetime. strptime ('2017-03-05 17:41:20 ',' % Y-% m-% d % H: % M: % s ')
D2 = datetime. datetime. strptime ('2017-03-02 17:41:20 ',' % Y-% m-% d % H: % M: % s ')
Delta = d1-d2
Print delta. days print delta
# Date of n days after today
Import datetime
Now = datetime. datetime. now ()
Delta = datetime. timedelta (days = 3)
N_days = now + delta
Print n_days.strftime ('% Y-% m-% d % H: % M: % s ')
The advantage of datetime is that it can realize convenient time operations, such as endTime-starTime, which is very convenient in time duration calculation.