datetime Profile
datetime advanced than a lot of time, can be understood based on time to encapsulate the datetime, provides functions you use, the interface module datetime more intuitive and easier to call
datetime module class
The class name | use |
---|---|
datetime | While there is time and date |
hour delta | Time span for calculating the main |
tzinfo | The time zone |
time | Focus only on time |
date | Only concerned Date |
datetime class function
1, datetime.now () function
DateTime.Now () function is used to obtain the current time
datetime.now () function usage:
datetime.datetime.now()
Datetime is datetime first module, the second module in a datetime datetime datetime class
type of typedatetime.datetime
import datetime
d1 = datetime.datetime.now()
print(d1)
Operating results as follows:
2020-01-27 19:22:32.745600
NOTE: The next .745600 milliseconds, it is negligible
2, datetime () function
datetime () function is used to obtain a specified time
datetime () function usage:
datetime.datetime(x)
Parameters x - - own seven parameters specified tuple type of time period
type of typedatetime.datetime
import datetime
d1 = datetime.datetime(1999, 10, 5, 12, 30, 54, 123456)
print(d1)
Operating results as follows:
1999-10-05 12:30:54.123456
3, strftime () function
strftime () function is used to time into a string
strftime () function usage:
datetime.datetime.now().strftime(x)
Parameters x - - your specified format symbol
datetime.datetime.now () refers to the current time
type of typestr
import datetime
d1 = datetime.datetime.now()
print(d1)
d2 = d1.strftime("%X")
print(d2)
Operating results as follows:
2020-01-28 17:08:34.932033
17:08:34
4, strptime () function
the strptime () function is used to format string into datetime objects
strptime () function usage:
datetime.datetime.strptime(p,x)
Parameter P - - conversion time required string
parameter x - - their specified formatting symbols
Note: The format conversion to be consistent with the string
type of typedatetime.datetime
import datetime
d1 = datetime.datetime.now()
print(d1)
d2 = d1.strftime("%X")
print(d2)
d3 = datetime.datetime.strptime(d2, "%X")
print(d3)
Operating results as follows:
2020-01-28 17:15:29.958495
17:15:29
1900-01-01 17:15:29
We note that here I did not start the conversion date, only transformed hours minutes seconds, so after the completion of the conversion Python did not know of the month, so for the initial 1900-01-01
5, the number of days interval
Days two time intervals directly subtracted to get the results
import datetime
d1 = datetime.datetime.now()
d2 = datetime.datetime(2000, 10, 25, 12, 45, 45, 123456)
d3 = d1 - d2
print(d3)
print(d3.days)
print(type(d3))
Operating results as follows:
7034 days, 4:38:06.433209
7034
<class 'datetime.timedelta'>
6, except for the number of days the number of seconds
I do not know how to introduce directly on the code
import datetime
d1 = datetime.datetime.now()
d2 = datetime.datetime(2000, 10, 25, 12, 45, 45, 123456)
d3 = d1 - d2
print(d3)
print(d3.seconds)
print(type(d3))
Operating results as follows:
7034 days, 4:40:41.623565
16841
<class 'datetime.timedelta'>