Python Date and Time--datetime

Source: Internet
Author: User
Tags date1 local time timedelta

Python Date and Time--datetime

Python provides several built-in modules for manipulating date times, such as Calendar,time,datetime. The interface provided by time is basically consistent with the C standard library time.h. The most widely used is DateTime, and the interface of the DateTime module is more intuitive and easier to invoke than the time module.

The DateTime module provides both simple and complex methods for date and time processing. While the date and time algorithms are supported, the focus of implementation is more efficient processing and formatting output. The module also supports time zone processing. This article learns about the DateTime library.

DateTime Library

There are four categories visible, date represents the day (year and year), DateTime (Month and day), and seconds ... The inheritance Date;time represents time (seconds, minutes ...). ); Timedelta indicates the time difference, and Tzinfo represents the timezone information (this chapter does not introduce).

Datetime.date

Date is the day, and the composition of the class is (years, months, days), and the corresponding year, month, and day are the attributes of the date class (type is int).

The range of year is [Minyear, maxyear], i.e. [1, 9999];
The range of month is [1, 12]. (The month is starting from 1, not starting from 0);
The maximum value of day is determined by the given year, month parameter. For example leap year February has 29 days, non-leap year 28 days;

    today () returns the day of the week,weekday () returns the current number of weeks, if Monday, returns 0, if Week 2, returns 1, and so on;Isoweekday () if Monday, returns 1, if Week 2, returns 2, And so on

    Isoformat () returns the date in ISO format, which is the string ' Yyyy-mm-dd ';strftime (...) method to customize the date notation (both time and datetime can be used), which is explained in detail after the post.

    date.replace (year/month/day): Generates a new Date object, substituting the attributes in the original object with the year, month, and day specified by the parameter. (The original object remains unchanged)

Some other methods refer to the documentation, the example code for the above method is as follows:

    

Datetime.time

Time, the composition of the class is (hour, minute, second, microsecond, time zone information), subtle, time zone information can default.

The time class provides instance methods and properties: Time.hour, Time.minute, Time.second, Time.microsecond: Hours, minutes, seconds, microseconds, Time.tzinfo: TimeZone information (not yet introduced);

where time.min = time (0, 0, 0, 0), Time.max = Timing (total, on, off, 999999); Time.resolution: the smallest unit of duration, here is 1 microseconds;

     Isoformat () returns the time in ISO format, which is the "HH:MM:SS" string;strftime (...) The method can also customize the time notation, which is explained in detail after the blog post.

Time.replace ([Hour[, Minute[, second[, microsecond[, Tzinfo] ] ] ) /c15>: generates a new time object, with parameters specified by time, minutes, seconds, Microseconds replace the attributes in the original object (the original object remains unchanged);

Some other methods refer to the documentation, the example code for the above method is as follows:

Datetime.datetime

DateTime is a combination of date and time, including all the information of date and times (usually DateTime is used).

Its constructor is as follows:datetime. datetime  (year, month, Day[, hour[, Minute[, second[, microsecond[, Tzinfo] ] []] ), the meanings of each parameter are the same as in the constructor for date and time, so be aware of the range of parameter values.

The class properties and methods defined by the DateTime class:

datetime.today () : Returns a DateTime object that represents the current local time;
datetime.now ([TZ]) : Returns a DateTime object that represents the current local time, and if parameter TZ is provided, gets the local time of the time zone referred to by the TZ parameter;
      datetime.utcnow () : Returns a DateTime object for the current UTC time;
datetime.fromtimestamp (timestamp[, TZ]) : Creates a DateTime object based on time timestamp, parameter TZ specifies time zone information;
datetime.date () : Gets the Date object, datetime.time ( : Gets the time object;
datetime. replace ([year[, month[, day[, hour[, minute[, S econd[, microsecond[, Tzinfo]]) : Usage same as date and time method;
datetime.co Mbine (date, time) : Creates a DateTime object based on date and time,
datetime.strptime (Date_ string, format) : Converts the format string to a DateTime object, and the method is described in detail after the post.

The time module is not covered in this article, but you need to understand a more important concept, the timestamp

  Floating point number, time ()

Return the current time in seconds since the Epoch.
Fractions of a second may be present if the system clock provides them.

Unix time, POSIX time, or Unix timestamp is the number of seconds that have elapsed since the epoch (January 1, 1970 00:00:00 UTC), regardless of leap seconds. For more information, please Google.

Import= time.time ()print timestamp

Examples of the above datetime methods are:

Timedelta

That represents the difference between a time:

Specific reference documents, this side will not be more elaborated:

Timedelta ([days[, seconds[, microseconds[, milliseconds[, minutes[, hours[, Weeks]] []]

All arguments is optional and default to 0. Arguments May is ints, longs, or floats, and may be positive or negative.


Only days, seconds and microseconds is stored internally. Arguments is converted to those units:

    • A millisecond is converted to microseconds.
    • A minute is converted to seconds.
    • An hour are converted to 3600 seconds.
    • A Week is converted to 7 days.

And days, seconds and microseconds were then normalized so, the representation was unique, with

    • 0 <= microseconds < 1000000
    • 0 <= seconds < 3600*24 (the number of seconds in one day)
    • -999999999 <= days <= 999999999

This is mainly supported for some timedelta operations of date, time, and DateTime, as follows:

Date has overloaded some operations, allowing us to do some of the following on dates:
Date2 = date1 + Timedelta # date plus an interval, returns a new Date object (Timedelta will be described below, representing the time interval)
Date2 = date1-timedelta # date interval, returns a new Date object
Timedelta = date1-date2 # Two date subtraction, returns a time interval object
Date1 < Date2 # Two dates to compare

Note: When working on a date, prevent the date from exceeding the range it can represent.

Time and datetime operations are identical to date, and there are no examples.

The formatting of time

datetime, date, and time all provide the strftime () method, which receives a format string that outputs a string representation of the DateTime.

The reference examples are as follows:

  

Python Date and Time--datetime

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.