The Python datetime module

Two constants

MAXYEAR:9999

MINYEAR:1

Five categories

datetime.datetime: Date Time Class 

datetime.date: Date Class 

datetime.time: time classes 

datetime.timedelta: time interval, i.e. the length of time between the two points 

datetime.tzinfo: the time zone information

datetime.datetime

parameter

datetime.datetime(year, month, day[, hour[, minute[, second[, microsecond[,tzinfo]]]]])

Examples of the method used

 

datetime.datetime.now () # 2019-01-02 10: 08: 07.611755 
# Returns the current system time 

datetime.datetime.now () the ctime () # 2 Wed Jan 2019 10:08:07. 
# Returns a date string, equivalent time module time.ctime (time.mktime (d.timetuple ())) 

datetime.datetime.now (). dATE () # 2019-01-02 
# returns the date of the current system time 

datetime. DateTime.Now () time () # 10:. 08: 07.611885 
# returns the current system time of the time portion 

datetime.datetime.fromtimestamp (1546393545.889359) # 2019-01-02 09: 45: 45.889359 
# the date and time stamp into 

datetime.datetime.now () Replace (year = 2020, = 12 is hour) # 2020-01-02 12 is:. 08: 07.611960 
# datetime based on the parameter to be modified, and returns the new class datetime 

datetime.datetime.now () .strftime ( 'the Y%%% B- D-% H:% M:% S') Jan-02-2019 10:08:07 # 
# by the date and time format into a format string

datetime.datetime.strptime ( 'On Apr-16-2017 21:01:35', 'the Y%%% B- D-% H:% M:% S') 
# string converted from the format of the date time format, 2017 -04-16 21:01:35

datetime.date

parameter

datetime.date(year, month, day)

Examples of the method used

datetime.date (= 2020 year, month The =. 11, Day =. 1) 2020-11-01 # 
# Create date class parameters 

datetime.date.fromtimestamp (1,546,393,545.889359) # 2019-01-02 
# classes based on the time stamp generation date 

datetime .date.today () # 2019-01-02 
# returns the current date 

datetime.date.today (). the ctime () # Wed Jan 2019 00:00:00 2 
# returns a string representing the date 

datetime.date.isoformat (datetime.date.today ()) # 2019-01-02 
datetime.date.isoformat (datetime.datetime.now ()) # 2019-01-02 
# returns the format MM-DD-YYYY 

datetime.date.today () .replace (year = 1999) # 1999-01-02 
# replace a given date, the date of the original unchanged 

datetime.date.today (). strftime ( '% b-% d-% Y') # Jan-02-2019 
# date formatted according to the specified format examples 

datetime.date.strftime (datetime.date.today (), '% b-% d-% Y% H:% M:% S')
# Formatted according to the specified format of the given date, Jan-02-2019 00:00:00

datetime.time

parameter

datetime.time([hour[, minute[, second[, microsecond[, tzinfo]]]]])

Examples of the method used

datetime.time = T (hour = 10, = 12 is minute, SECOND =. 11) 
t.replace (hour = 2) 02:12:11 # 
# objects to generate a new time, specified time parameter corresponding to the object instead of the original properties 

t.isoformat () # 10:12:11 
# returns a string ISO 8601 format 

t.strftime ( '%% H. M. S%') # 10.12.11 
# output time according to the specified format string

datetime.timedelta

parameter

All parameters are optional and can be an integer or floating-point numbers, positive or negative.

datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

Examples of the method used

datetime.datetime.now = TIME_1 () 
time_2 = datetime.datetime.now () 
time_2 - TIME_1 
# timedelta example a 

(time_2 - TIME_1) .total_seconds () 
# total number of seconds between two time

datetimeAnd class date, timeconversion between classes

datetime.date.today = D () 
t = datetime.time (= 10 hour, minute =. 11, 12 is = SECOND) 

dt = datetime.datetime.combine (D, t) 
# The date class d, time t based connection, datetime class composition dt, date must be placed in front of the class 

dd = dt.date () 
# extract date from datetime class class 

TT = dt.time () 
# extracted from the time-based class datetime

The date and time formatting symbols

symbol Show
%y Two digits representing the year(00-99)
%Y Four digits representing the year(0000-9999)
%m month(01-12)
%d One day during the month(01-31)
%H 24Hour Hours(00-23)
%I 12Hour Hours(01-12)
%M The number of minutes(00-59)
%S The number of seconds(00-59)
   
%a Local simplify week name(Wed)
%A Local full weekday name(Wednesday)
%b Local simplify the name of the month(Jan)
%B Local full month name(January)
%c Local representation appropriate date and time representation(Tue Aug 16 21:30:00 2018)
%j One day of the year(001-366)
%p Local AMor PMequivalent symbol
%U In a few weeks a year, starting with Sunday(00-53)
%w week(0-6)
%W In a few weeks a year, starting with Monday(00-53)
%x Corresponding date local representation
%X Represents the corresponding local time
%Z The current time zone name
%% %No. itself

 

 

 

 

Guess you like

Origin www.cnblogs.com/jeemzz/p/11423220.html