Python standard library: datetime module

Source: Internet
Author: User
Tags time zones iso 8601

Tag: Time contains the import ORM str instead of including bin Port

Original address: http://www.bugingcode.com/blog/python_datetime.html

datatime Module questions share some functions that deal with date, time, and time intervals. This module uses object-oriented interaction to replace the time function of the shaping/tuple type in the Times module.

All the types in this module are new classes that can be inherited and extended from Python.

This module contains the following types:

    • datetime represents the date and time of day

    • Date represents the day, between 1 and 9999

    • time stands for times and independent dates.

    • Timedelta stands for two time or date intervals

    • Tzinfo Implementing time zone support

The datetime type represents the date and time of a particular time zone, and unless otherwise specified, the DateTime object uses "Naive time", which means that datetime in the program is associated with the timezone in which it is located. The datetime module provides support for some time zones.

Datetime

Given a time to create a DateTime object, you can use the datetime constructor:

import datetimenow = datetime.datetime(2003, 8, 4, 12, 30, 45)print nowprint repr(now)print type(now)print now.year, now.month, now.dayprint now.hour, now.minute, now.secondprint now.microsecond

The results are as follows:

$ python datetime-example-1.py2003-08-04 12:30:45datetime.datetime(2003, 8, 4, 12, 30, 45)<type ‘datetime.datetime‘>2003 8 412 30 450

It is important to note that the default string represents the ISO 8601-style timestamp.

You can also use the built-in factory function to create a DateTime object (all of these functions provide a class method, not a module function).

import datetimeimport timeprint datetime.datetime(2003, 8, 4, 21, 41, 43)print datetime.datetime.today()print datetime.datetime.now()print datetime.datetime.fromtimestamp(time.time())print datetime.datetime.utcnow()print datetime.datetime.utcfromtimestamp(time.time())

The results are as follows:

$ python datetime-example-2.py2003-08-04 21:41:432003-08-04 21:41:43.5220002003-08-04 21:41:43.5220002003-08-04 21:41:43.5220002003-08-04 19:41:43.5320002003-08-04 19:41:43.532000

As in these examples, the default format of the time object is the ISO 8601-style string: "Yyyy-mm-dd hh:mm:ss", the milliseconds are optional and can or may not be.

The datetime type provides another format method, including a highly versatile strftime method (which can be seen in more detail in the time module).

import datetimeimport timenow = datetime.datetime.now()print nowprint now.ctime()print now.isoformat()print now.strftime("%Y%m%dT%H%M%S")$ python datetime-example-3.py2003-08-05 21:36:11.590000Tue Aug  5 21:36:11 20032003-08-05T21:36:11.59000020030805T213611
Date and time

Date represents the dates part of a DateTime object.

import datetimed = datetime.date(2003, 7, 29)print dprint d.year, d.month, d.dayprint datetime.date.today()

The results are as follows:

$ python datetime-example-4.py2003-07-292003 7 292003-08-07

time is similar, he represents a part of the moment, in milliseconds.

import datetimet = datetime.time(18, 54, 32)print tprint t.hour, t.minute, t.second, t.microsecond

The results are as follows:

$ python datetime-example-5.py18:54:32

datetime provides an extension method for date and Time objects, as well as a class method that transforms two objects onto a temporal object.

import datetimenow = datetime.datetime.now()d = now.date()t = now.time()print nowprint d, tprint datetime.datetime.combine(d, t)

The results are as follows:

$ python datetime-example-6.py2003-08-07 23:19:57.9260002003-08-07 23:19:57.9260002003-08-07 23:19:57.926000

Reprint please indicate: A cat learns to program

More tutorials: A-python basic tutorial for a cat learning program

Python standard library: datetime module

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.