I. INTRODUCTION of DATETIME
DateTime is the standard library for Python to process dates and times
Second, import datetime date time processing Standard library
# datetime is a datetime module, which includes a DateTime class with the same name
From datetime import datetime
Third, get the current date and time information
# Get current datetime information
now = DateTime.Now ()
Iv. specified date and time
DT = DateTime (2015,3,4,12,12,23)
V. Convert date time to Unix time suffix
# convert datetime objects to UNIX time prefixes
Dt.timestamp ()
Vi. converting a time suffix to a date time
# Convert a time suffix to a date time
t = 1565455645
DT = Datetime.fromtimestamp (t)
Seven, string conversion to DateTime
# Convert String to Datetiem
# One of the rear is the date-time matching pattern: see Https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior for details
# Note that there is no time zone information after the conversion
Cday = Datetime.strptime ("2015-6-1 18:56:52", "%y-%m-%d%h:%m:%s")
Eight, date time converted to string
now = DateTime.Now ()
str = now.strftime ("%a,%b,%d%h:%m")
ix. addition and subtraction of datetime
# Add and subtract to date time need to import Timedalta this class
From datetime import Datetime,timedalta
now = Datetime.datetime ()
Now + Timedalta (hours=10) # on the basis of the original time, after another 10 hours between
now = Timedalta (day=2,hours=10) #时间过后两天, time after 10 hours after the hour
Ten, get local time
Import time
# Get local time
LocalTime = Time.localtime (Time.time ())
Print ("local time:", localtime)
Struct_time Time Tuple:
Tm_year
Tm_mon
Tm_mday
Tm_hour
Tm_min
Tm_sec
Tm_wday
Tm_yday
Tm_isdst
Xi. get a calendar of a month
Import Calendar
Cal = Calendar.month (year, month)
Print (CAL)
Python Learning notes (18)