Type of time in Python, relationship of datetime type and conversion to each other

Source: Internet
Author: User
Tags timedelta

One. Time module

Time module provides functions for various operating times
There are generally two ways to represent time:
The first is the way the timestamp is (relative to the offset from 1970.1.1 00:00:00 in seconds), the timestamp is unique

Python code
    1. #当前时间的时间戳
    2. In [9]: time.time ()
    3. out[9]: 1376102328.536908

The second is represented as an array of (Struct_time), a total of nine elements, respectively, that the same timestamp of the struct_time will vary depending on the time zone

Python code
    1. In [2]: time.localtime ()
    2. out[2]: Time.struct_time (tm_year=, tm_mon=8, tm_mday=, tm_hour=, tm_min=, tm_sec= tm_wday= 5,tm_yday=222, tm_isdst=0)

Gmtime () and mktime () can freely convert two time representation methods

Python code
    1. In []: Time.gmtime (Time.time ())
    2. out[]: Time.struct_time (tm_year=, tm_mon=8, tm_mday=, tm_hour=2, tm_min=, tm_sec= Si , tm_wday=5, tm_yday=222, tm_isdst=0)
    3. In [23°c]: Time.mktime (Time.localtime ())
    4. out[]: 1376102845.0

Strftime () can convert the Struct_time type freely into character type

Python code
    1. in [+]: Time.strftime ("%y%m%d", Time.localtime ())
    2. out[]: ' 20130810 '

Strptime (string, format) converts a time string to an array of time based on the specified format character

Python code
    1. in [+]: time.strptime (' 20130810 ', "%y%m%d")
    2. out[]: Time.struct_time (tm_year=, tm_mon=8, tm_mday=, tm_hour=0, tm_min=0, tm_sec= 0, tm_wday=5, tm_yday=222, tm_isdst=-1)

Two. DateTime module

Python code
    1. in [+]: Datetime.datetime.now ()
    2. out[]: datetime.datetime (8, ten, ten, Up, Ten, 611490) /c4>

Python provides several built-in modules for manipulating date times, like Calendar,time,datetime. Time module. The interface of the DateTime module is more intuitive and easier to invoke than the time module.

The DateTime module defines the following classes:

    • Datetime.date: A class that represents a date. The commonly used attributes are year, month and day;
    • Datetime.time: A class that represents time. The commonly used properties are hour, minute, second, microsecond;
    • Datetime.datetime: Represents the DateTime.
    • Datetime.timedelta: Represents the time interval, which is the length between two points in time.
    • Datetime.tzinfo: Related information about the time zone.

DateTime is a DateTime object that represents the date time

Python code
    1. in [+]: Datetime.datetime.now ()
    2. out[]: datetime.datetime (8, ten, ten, Up, Ten, 611490) /c4>

The Strftime method is available in datetime to convert a datetime date to a string:

Java code
    1. in [+]: Datetime.datetime.now (). Strftime ("%y%m%d")
    2. out[]: ' 20130810 '

Datetime.strptime (date_string, format): Converts a format string to a DateTime object

Java code
    1. in [+]: Datetime.datetime.strptime ("20130810", "%y%m%d")
    2. out[]: datetime.datetime ( 8, 0, 0)

DateTime overloads some operations, which allow us to do some of the following on dates:

    • DateTime2 = datetime1 + Timedelta # date plus an interval, returns a new Date object (Timedelta will be described below, representing the time interval)
    • datetime2 = datetime1-timedelta # date interval, returns a new Date object
    • Timedelta = date1-date2 # Two date subtraction, returns a time interval object
    • Datetime1 < DateTime2 # Two dates to compare
Python code
  1. In []: time1 = Datetime.datetime.now ()
  2. In [time2]: = Datetime.datetime.now ()
  3. In []: time2 > Time1
  4. out[]: True
  5. In [All]: Datetime.datetime.now ()-Datetime.timedelta (days = 7)
  6. out[]: datetime.datetime ( 8, 3, one, one, 881810)

Three. Conversion between Time and DateTime

Date.fromtimestamp (timestamp): Returns a Date object based on the given time timestamp

Python code
    1. In [Wuyi]: Datetime.datetime.fromtimestamp (Time.time ())
    2. out[]: datetime.datetime ( 8, ten, one, +, 842812) /c4>

The conversion between Time_struct and DateTime can be done by means of an intermediate state string.

--------------------------------------------------------------------------------------------------------------- ------------

Reference documents:



Type of time in Python, relationship of datetime type and conversion to each other

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.