Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use datetime of Python

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to use datetime of Python". In daily operation, I believe many people have doubts about how to use datetime of Python. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use datetime of Python". Next, please follow the editor to study!

1. Datetime.date class

The date object defined by the datetime.date class represents the date (such as 2021.10.30)

1.1datetime.date class format class datetime.date (year,month,day)

Parameter: cannot be default. If the parameter does not exist, a ValueError exception is thrown

Value range of year parameter: [MINYEAR, MAXYEAR]

Value range of month parameter: [119912]

Value range of day parameter: [1, number of days corresponding to a given year and month]

The maximum date represented by the 1.2datetime.date class method and property class method / attribute, the minimum date represented by the 9999-12-31date.mindate object, the maximum unit of the date represented by the 0001-01-01date.resolutiondate object, and the day date.today () gets the current local date date.fromtimestamp (timestamp) to convert the timestamp into a date

Date object methods and properties

The object method / attribute function d.year represents the year d.month represents the month d.day represents the day d.replace (year, [, month, [, day]]) generates a new date d.timetuple () returns the corresponding structured object d.toordinal () returns the date from the beginning of 0001-01-01, the nth day d.weekday () returns the day of the week, and [0je 6] 0 represents the day of the week d.isoweekday returns. [1datetime.date.today 7] 1 represents the date string d.strftime (format) in the form of tuple d.isoformat () 'YYYY-MM-DD' in the form of d.isocalendar () (year, weekday, isoweekday) on Monday. Returns the date string import datetimedat = datetime.date (2021, 10pl 30) print ("datetime.date.today () class method", datetime.date.today ()) print ("date object year property:", dat.year) print ("date object month property:" Dat.month) print ("date object day attribute:", dat.day) print ("date object replace:", dat.replace (2022)) print ("date object timetuple:", dat.timetuple ()) print ("date object weekday:", dat.weekday ()) print ("date object isoweekday:", dat.isoweekday () print ("date object isocalendar:", dat.isocalendar ()) print ("date object isoformat:", dat.isoformat ()) print ("date object strftime:" Dat.strftime ("% d-%m-%Y"))

2. Datetime.time class

The object of the datetime.time class represents the time of a certain day somewhere, independent of any particular date, and can be adjusted through the tzinfo object.

2.1datetime.time class format class datetime.time (hour=0,minute=0,second=0,mircrosecond=0,tzinfo=Noe,*,fold=0)

Parameters: all parameters are optional

Value range of hour parameter: [0Power23]

Value range of minute parameter: [0759]

Value range of second parameter: [0759]

Value range of microsecond: [014000000]

Tzinfo: it can be None or an instance of a tzinfo subclass

2.2datetime.time class methods and attribute class methods / attributes function the time.maxtime class represents the maximum time, the time (23,59,59, 999999) time.mintime class represents the minimum time, and the minimum unit of time (0,0,0) time.resolution time 1 microsecond 2.3datetime.time class method and attribute instance method / attribute action t.hour representation t.minute represents minute t.second t.microsecond represents microsecond t.tzinfo returns the tzinfo object t.replace (hour, mintue, [) that passes the time construction method ]]) generate a new time t.isoformat () return a 'HH:MM:SS.%f' format time string t.strftime () return the specified format time string 3. Datetime.datetime class

The datetime.datetime class is a subclass of datetime.date, an object that contains all the information about time and date

3.1datetime.datetime class definition format class datetime.datetime (year,month,day,hour=0,minute=0,second=0,mircrosecond=0,tzinfo=Noe,*,fold=0)

Parameters: year,month and day parameters are required

Value range of year parameter: [MINYEAR, MAXYEAR]

Value range of month parameter: [119912]

Value range of day parameter: [1, number of days corresponding to a given year and month]

Value range of hour parameter: [0Power23]

Value range of minute parameter: [0759]

Value range of second parameter: [0759]

Value range of microsecond: [014000000]

Tzinfo: it can be None or an instance of a tzinfo subclass

Fold in [0,1]

3.2datetime.datetime class methods and property class methods / attributes function datetime.today () f to return the datetime object datetime.now ([tz]) of the date time of the current period and return the datetime object of the specified time zone date time If the tz parameter is not specified, the system time datetime.utcnow () returns the current utc date time datetime object datetime.fromtimestamp (timestamp [, tz]) creates a datetime object based on the specified timestamp datetime.utcfromtimestamp (timestamp) creates a datetime object based on the specified timestamp datetime.combine (date, time) integrates the specified date and time objects into a datetime object datetime.strptime (date_str, format) converts the time string into a datetime object

Datetime.datetime class instance methods and properties

Instance method / attribute action dt.year represents year dt.month represents month dt.day represents day dt.minute represents minute dt.second represents second dt.microsecond represents microsecond dt.tzinfo represents time zone dt.date () gets date object corresponding to datetime object dt.time () gets time object corresponding to datetime object, tzinfo represents Nonedt.timetz () gets time object corresponding to datetime object Tzinfo is the same dt.replace as the tzinfo of the datetime object ([year [, month [, day [, hour [, minute [, second [, microsecond] Tzinfo]) generate a new datetime object dt.timetuple () returns the tuple corresponding to the datetime object (excluding tzinfo) dt.utctimetuple () returns the tuple of the utc time corresponding to the datetime object (excluding tzinfo) dt.timestamp () returns the timestamp corresponding to the datetime object dt.toordinal () returns the date from 0001-01-01 the number of days dt.weekday () returns the day of the week [0,6], 0 means that Monday dt.isocalendar () returns a tuple in the format: (year, weekday, isoweekday) dt.isoformat ([sep]) returns a string dt.ctime () in the format of'% Ymuri% mmi% d% HRV% MV% S.% f 'equivalent to time.ctime (time.mktime (d.timetuple ()) dt.strftime (format) of time module) returns a time string of the specified format 4. Datetime.timedelta class

The object defined by the datetime.timedelta class represents the interval between two date or time

4.1datetime.timedelta class definition format class datetime.timedelta (days=0,seconds=0,microseconds=0,milliseconds=0,hours=0,weeks=0)

Parameters: all parameters are optional and default to 0

Parameter type: integer or floating point number, positive or negative number

Only days, seconds and microseconds will be stored internally

Conversion formula:

1seconds = 1000microsends

1minutes = 60seconds

1hours = 3600seconds

1weeks = 7days

Value range of seconds: [0pc86399]

Value range of microseconds: [0999999]

Value range of days: [- 99999999999999999]

4.2datetime.timedelta class methods and attribute class methods / attributes function timedelta.mintimedelta (- 999999999) timedelta.maxtimedelta (days=999999999, hours=23, minutes=59, seconds=59, microseconds=999999) timedelta.resolutiontimedelta (microseconds=1)

Datetime.timedelta class instance methods and properties

The total number of seconds contained in the time difference of instance method / attribute in td.days days, [- 999999999, 999999999] td.seconds seconds [0,86399] td.microseconds microseconds [0,999999] td.total_seconds (), equivalent to: td / timedelta (seconds=1) 5. Datetime.tzinfo class and datetime.timezone class

The datetime.tzinfo class is an abstract base class that is not directly instantiated.

Subclass use cases of tzinfo capture information about a specific time zone

An instance of tzinfo can be passed to the constructor of datetime and time objects

The datetime.timezone class is a subclass of tzinfo, and each instance of it represents a time zone defined by a fixed time difference from UTC.

5.1datetime.timezone class definition format class datetime.timezone (offset,name=None)

Parameters:

Offset must now specify a timedelta object, indicating that the time difference between the local time and the UTC is in the range of [- 24. 24].

Name: is optional. If you specify that it must be a string, it will be used as the return value of the datetime.tzname () method

The 5.2datetime.timezone class method and property class method / property function timezone.utcoffset (dt) returns the fixed value specified when the timezone instance is constructed. Timezone.tzname (dt) returns the fixed value specified when the timezone instance is constructed timezone.dst (dt) always returns nonetimezone.formutc (dt) returns dt+uoffsettimezone.utc returns UTC time zone, so the study on "how to use the datetime of Python" is over. I hope to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report