Functions of the datetime module in Python

Source: Internet
Author: User
Tags iso 8601 iso 8601 format timedelta
>>> Dir (datetime) ['maxyear', 'minyear', '_ Doc _', '_ name _', '_ package __', 'date', 'datetime', 'datetime _ CAPI ', 'time', 'timedelta', 'tzinfo']

Data contained:
Maxyear -- maximum year that can be expressed
Minyear -- minimum year that can be expressed
Datetime_capi -- the object accessed by C language API, which can be help (datetime. datetime_capi)
 
Objects included:
Date -- date object
Datetime -- Date and Time object
Time -- time object
Timedelta -- date time interval object

Tzinfo -- time zone information object

>>> Dir (datetime. date) ['_ add _', '_ class _', '_ delattr _', '_ Doc __', '_ eq _', '_ format _', '_ Ge _', '_ getattribute _', '_ GT __', '_ Hash _', '_ init _', '_ Le _', '_ LT _', '_ ne __', '_ new _', '_ Radd _', '_ reduce _', '_ performance_ex _', '_ repr __', '_ rsub _', '_ setattr _', '_ sizeof _', '_ STR _', '_ Sub __', '_ subclasshook _', 'ctime', 'day', 'fromordinal', 'fromtimestamp', 'isocalendar ', 'isoformat', 'isoekday', 'max ', 'Min', 'month', 'replace ', 'resolution', 'strftime', 'timetuple', 'today', 'toordinal', 'weekday', 'Year']

Data contained:
Day
Month -- month value
Year -- year value
Max = datetime. Date (9999, 12, 31) -- maximum value of year, month, and day
Min = datetime. Date (1, 1, 1) -- minimum value of year, month, and day
Resolution = datetime. timedelta (1) -- The interval is measured in 1 day.

Included functions:
Date (year, month, day) -- constructor, takes the year, month, and day parameters, returns a date object
Ctime () -- returns the Time Representation of the string format. Parameter not accepted
Fromordinal () -- accepts the number of days of an integer and returns the date object of the number of years, months, and days equivalent to the number of days.
Fromtimestamp () -- receives a floating point Timestamp and returns a date object of the year, month, and day corresponding to the value from January 1, 1970 + timestamp.
Isocalendar () -- returns an ISO-standard three-element group in the calendar format, representing ISO year, week number, and weekday. No parameters are received.
Isoformat () -- returns an ISO date string. The format is YYYY-MM-DD and parameters are not received
Isoweekday () -- returns the week representation in ISO format. Monday = 1... Sunday = 7, do not receive Parameters
Replace () -- returns a new date object that replaces the specified date field. The three optional parameters are year, month, and day. Note that replacement produces new objects without affecting the original date objects.
Strftime () -- returns a formatted string date. Receives a format parameter, such as '% Y-% d-% M'
Timetuple () -- returns a time format object of time. Equivalent to time. localtime ()
Today () -- returns the current date or datetime object. It is equivalent to fromtimestamp (Time. Time ())
Toordinal () -- returns the number of days from the Gregorian calendar to the present. In January 1
Weekday () -- returns the period represented by the date. Monday = 0... Sunday = 6

 
>>> Dir (datetime. time) ['_ class _', '_ delattr _', '_ Doc _', '_ eq __', '_ format _', '_ Ge _', '_ getattribute _', '_ GT _', '_ Hash __', '_ init _', '_ Le _', '_ LT _', '_ ne _', '_ new __', '_ nonzero _', '_ reduce _', '_ performance_ex _', '_ repr _', '_ setattr __', '_ sizeof _', '_ STR _', '_ subclasshook _', 'dst ', 'hour', 'isoformat', 'max ', 'microsecond', 'Min', 'minute ', 'replace', 'resolution', 'second', 'strftime', 'tzinfo', 'tzname', 'utcoffset']

Data contained:
Hour -- hour indicates a value
Microsecond -- microsecond indicates a value
Minute -- Minute indicates the value
Second -- Second indicates the value
Tzinfo -- Time Zone indicates a value
Max = datetime. Time (23, 59, 59,999 999) -- The maximum time indicates a value.
Min = datetime. time (0, 0) -- minimum time indicates a value.
Resolution = datetime. timedelta (0, 0, 1) -- time interval in minutes

Included functions:
Time ([hour [, minute [, second [, microsecond [, tzinfo]) -- constructor returns a time object. All parameters are optional.
DST () -- returns the description of the time zone. If the instance does not have the tzinfo parameter, null is returned.
Isoformat () -- returns an ISO 8601 string, HH: mm: ss [. mmmmmm] [+ hh: mm].
Replace () -- returns a new time object that replaces the specified time field. Four optional parameters: hour, minute, second, And microsecond. Note that replacement produces new objects without affecting the original time objects.
Strftime () -- receives a format parameter and returns a string of the corresponding format. For example, '% H: % m: % s'
Tzname () -- returns the name of the time zone. If the instance does not have the tzinfo parameter, null is returned.
Utcoffset () -- return self. tzinfo. utcoffset (Self)

>> Dir (datetime. datetime) ['_ add _', '_ class _', '_ delattr _', '_ Doc __', '_ eq _', '_ format _', '_ Ge _', '_ getattribute _', '_ GT __', '_ Hash _', '_ init _', '_ Le _', '_ LT _', '_ ne __', '_ new _', '_ Radd _', '_ reduce _', '_ performance_ex _', '_ repr __', '_ rsub _', '_ setattr _', '_ sizeof _', '_ STR _', '_ Sub __', '_ subclasshook _', 'astimezone ', 'combine', 'ctime', 'date', 'day', 'dst', 'fromordinal', 'fromtimestamp ', 'hour', 'isocalendar ', 'isoformat', 'isoekday', 'max', 'microsecond', 'Min', 'minute ', 'month', 'right ', 'replace ', 'resolution', 'second', 'strftime', 'strptime', 'time', 'timetuple', 'timetz', 'today', 'toordinal ', 'tzinfo', 'tzname', 'utcfromtimestamp', 'utcnow ', 'utcoffset', 'utctimetuple', 'weekday', 'Year'] 

Data inherited from date:
Day
Month
Year

Functions inherited from date:
Fromordinal (...)
Isocalendar (...)
Isoweekday (...)
Strftime (...)
Today (...)
Toordinal (...)
Weekday (...)

Data contained:
Hour
Microsecond
Minute
Second
Tzinfo
Max = datetime. datetime (9999, 12, 31, 23, 59, 59,999 999)
Min = datetime. datetime (1, 1, 1, 0, 0)
Resolution = datetime. timedelta (0, 0, 1)

Included functions:
Datetime (year, month, day [, hour [, minute [, second [, microsecond [, tzinfo]) -- constructor, returns a datetime object. Year, month, and day are required.
Astimezone (...) -- returns the timezone object converted to the local time zone based on the given timezone object.
Combine (...) -- returns a datetime object of the corresponding value after merging the given date and time objects.
Ctime (...) -- returns a string in ctime format.
Date (...) -- returns the date object with the same year, month, and day.
DST (...) -- returns self. tzinfo. DST (Self ).
Fromtimestamp (...) -- returns a datetime object based on the timestamp value.
Isoformat (...) -- returns a string in ISO 8601 format, such as: YYYY-MM-DDTHH: mm: ss [. Mmmm] [+ hh: mm]
Now (...) -- returns the current local time based on the given time zone.
Replace (...) -- returns a new datetime object that replaces the specified datetime field.
Strptime (...) -- returns a corresponding datetime object based on string and format2.
Time (...) -- returns a time object with the same time but without tzinfo.
Timetuple (...) -- returns a time element, equivalent to time. localtime ().
Timetz (...) -- returns a time object with the same time and tzinfo.
Tzname (...) -- return self. tzinfo. tzname (Self ).
Utcfromtimestamp (...) -- returns the datetime object of the UTC timestamp. The timestamp value is time. Time ()
Utcnow (...) -- returns the datetime object of the current UTC time.
Utcoffset (...) -- return self. tzinfo. utcoffset (Self ).
Utctimetuple (...) -- returns the UTC time tuples, equivalent to time. localtime ().


Note:
The datetime. Time object calls the basic method of the time module. And encapsulate it.
The datetime. date object also calls the basic method of the time module and encapsulates it.
The datetime. datetime object inherits the datetime. Date class and calls the method of the time module.

 
>>> Dir (timedelta) ['_ ABS _', '_ add _', '_ class _', '_ delattr __', '_ Div _', '_ Doc _', '_ eq _', '_ floordiv _', '_ format __', '_ Ge _', '_ getattribute _', '_ GT _', '_ Hash _', '_ init __', '_ Le _', '_ LT _', '_ Mul _', '_ ne _', '_ neg __', '_ new _', '_ nonzero _', '_ POS _', '_ Radd _', '_ rdiv __', '_ reduce _', '_ performance_ex _', '_ repr _', '_ rfloordiv _', '_ rmul __', '_ rsub _', '_ setattr _', '_ sizeof _', '_ STR _', '_ Sub __', '_ subclasshook _', 'days ', 'max', 'microseconds', 'Min', 'resolution', 'seconds', 'total _ seconds']

Data contained:
Days -- days
Microseconds -- number of microseconds (> = 0 and less than 1 second ).
Seconds -- seconds (> = 0 and less than 1 day ).

Max = datetime. timedelta (999999999,863 99, 999999)
Min = datetime. timedelta (-999999999)
Resolution = datetime. timedelta (0, 0, 1)
 
Included functions:
Timedelta () -- constructor that receives three parameters: Day, second, And microsecond.
Total_seconds () -- The total number of seconds of the time interval

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.