A detailed introduction to the Python time module datetime

Source: Internet
Author: User
This article will share with you how to use datetime in the time module of python, precautions and simple examples, if you have any need, refer to the following section to share with you the usage of datetime in the time module of python. the precautions and simple examples are as follows:

The datetime module uses the collection of the date and time modules. datetime has two constants, MAXYEAR and MINYEAR, respectively 9999 and 1.

The datetime module defines five classes, which are

1. datetime. date: indicates the date class.

2. datetime. datetime: indicates the date and time class.

3. datetime. time: indicates the time class.

4. datetime. timedelta: indicates the interval between two time points.

5. datetime. tzinfo: time zone information

1. First, let's take a look at the datetime. date class:

The date class has three parameters: datetime. date (year, month, day), and year-month-day is returned.

Method:

1. datetime. date. ctime (), the return format is as follows: Sun Apr 16 00:00:00 2017

2. datetime. date. fromtimestamp (timestamp), returns a date object based on the given time period. datetime. date. today () has the same effect

3. datetime. date. isocalendar (): returns the tuples in the format of (year, month, day), (2017, 15, 6)

4. datetime. date. isoformat (): return format such as YYYY-MM-DD

5. datetime. date. isoweekday (): returns the week (0-6) of the given date, Monday = 0, Sunday = 6

6. datetime. date. replace (year, month, day): replaces the given date without changing the original date.

7. datetime. date. strftime (format): format the date and time according to the specified format.

8. datetime. date. timetuple (): returns the time. struct_time object corresponding to the date.

Time. struct_time (tm_year = 2017, tm_mon = 4, tm_mday = 15, tm_hour = 0, tm_min = 0, tm_sec = 0, tm_wday = 5, tm_yday = 105, tm_isdst =-1)

9. datetime. date. weekday (): returns the week of the date.

Time and date formatting symbols in python:

% Y two-digit year (00-99)

% Y indicates the four-digit year (000-9999)

% M month (01-12)

One day in % d Month (0-31)

% H Hour in 24-hour format (0-23)

% I 12-hour (01-12)

% M minutes (00 = 59)

% S seconds (00-59)

% A local simplified week name

% A Local full week name

% B local simplified month name

% B local full month name

% C local date and time

% J one day in the year (001-366)

% P local equivalent of A. M. or P. M.

% U number of weeks in a year (00-53) Sunday is the start of the week

% W week (0-6), Sunday is the beginning of the week

% W number of weeks in a year (00-53) Monday is the start of the week

% X local date representation

% X local time representation

% Z current time zone name

% Itself

II. Take a look at the datetime time class

The time class has five parameters: datetime. time (hour, minute, second, microsecond, tzoninfo), returns 08:29:30

1. datetime. time. replace ()

2. datetime. time. strftime (format): returns the time according to the format.

3. datetime. time. tzname (): returns the time zone name.

4. datetime. time. utcoffset (): returns the time offset of the time zone.

3. datetime type

Datetime class has many parameters, datetime (year, month, day [, hour [, minute [, second [, microsecond [, tzinfo]), return year, month, day, hour, minute, second

Datetime. datetime. ctime ()

Datetime. datetime. now (). date (): returns the date part of the current date and time.

Datetime. datetime. now (). time (): returns the time part of the current date and time.

Datetime. datetime. fromtimestamp ()

Datetime. datetime. now (): returns the current system time

Datetime. datetime. replace ()

Datetime. datetime. strftime (): convert from date format to string format

Datetime. datetime. now (). strftime ('% B-% d-% Y % H: % M: % S ')

'Apr-16-2017 21:01:35'

Datetime. datetime. strptime (): converts string format to date format.

Datetime. datetime. strptime ('apr-16-2017 21:01:35 ',' % B-% d-% Y % H: % M: % S ')
21:01:35
IV. timedelta class of datetime

Datetime. datetime. timedelta is used to calculate the difference between two dates. for example:

>>> a=datetime.datetime.now()>>> b=datetime.datetime.now()>>> adatetime.datetime(2017, 4, 16, 21, 21, 20, 871000)>>> bdatetime.datetime(2017, 4, 16, 21, 21, 29, 603000)>>> b-adatetime.timedelta(0, 8, 732000)>>> (b-a).seconds8

Or

Time1 = datetime. datetime (2016, 10, 20) time2 = datetime. datetime (2015, 11, 2) "calculate the number of days difference" print (time1-time2 ). days "calculates the number of seconds between two dates" print (time1-time2 ). total_seconds ()

The above is the detailed description of the Python time module datetime. For more information, see other related articles in the first PHP community!

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.