Python date function, reprinted python date function

Source: Internet
Author: User
Tags timedelta

(Reprinted) python date function, reprinted python date function

Reprinted on http://www.cnblogs.com/emanlee/p/4399147.html

All Date and Time APIs are in the datetime module.

1. format the date output datetime => string

Import datetime

Now = datetime. datetime. now ()

Now. strftime ('% Y-% m-% d % H: % M: % s ')

Output

'2017-04-07 19:11:21'

Strftime is an instance method of the datetime class.

 

2. format the date output string => datetime

Import datetime

T_str = '2017-04-07 19:11:21'

D = datetime. datetime. strptime (t_str, '% Y-% m-% d % H: % M: % s ')

Strptime is a static method of the datetime class.

 

3. Date comparison operation

In the datetime module, there is a timedelta class. The objects of this class are used to represent a time interval, for example, the difference between two dates or times.

Constructor:

Import datetime

Datetime. timedelta (days = 0, seconds = 0, microseconds = 0, milliseconds = 0, minutes = 0, hours = 0, weeks = 0)

All parameters have the default value 0. These parameters can be int, float, positive, or negative.

You can use timedelta. days and tiemdelta. seconds to obtain the corresponding time value.

Instances of the timedelta class support addition, subtraction, multiplication, division, and other operations. The result is also an instance of the timedelta class. For example:

Import datetime

Year = datetime. timedelta (days = 365)

Ten_years = year * 10

Nine_years = ten_years-year

At the same time, the date, time, and datetime classes also support addition and subtraction operations with timedelta.

Datetime1 = datetime2 + timedelta

Timedelta = datetime1-datetime2

In this way, you can easily implement some functions.

 

4. How many days do the two dates differ.

Import datetime

D1 = datetime. datetime. strptime ('2017-03-05 17:41:20 ',' % Y-% m-% d % H: % M: % s ')

D2 = datetime. datetime. strptime ('2017-03-02 17:41:20 ',' % Y-% m-% d % H: % M: % s ')

Delta = d1-d2

Print delta. days

Output: 3

 

5. The date of n days after today.

Import datetime

Now = datetime. datetime. now ()

Delta = datetime. timedelta (days = 3)

N_days = now + delta

Print n_days.strftime ('% Y-% m-% d % H: % M: % s ')

Output:

19:16:34

 

# Coding = UTF-8

Import datetime

Now = datetime. datetime. now ()

Print now

 

# Convert a date to a string

Datetime => string

Import datetime

Now = datetime. datetime. now ()

Print now. strftime ('% Y-% m-% d % H: % M: % s ')

 

# Convert a string to a dateString => datetime

Import datetime

T_str = '2017-03-05 16:26:23'

D = datetime. datetime. strptime (t_str, '% Y-% m-% d % H: % M: % s ')

Print d

# There is a timedelta class in the datetime module. The objects of this class are used to represent a time interval, for example, the difference between two days # or time.

 

# Calculate the interval between two dates

Import datetime

D1 = datetime. datetime. strptime ('2017-03-05 17:41:20 ',' % Y-% m-% d % H: % M: % s ')

D2 = datetime. datetime. strptime ('2017-03-02 17:41:20 ',' % Y-% m-% d % H: % M: % s ')

Delta = d1-d2

Print delta. days print delta

 

# Date of n days after today

Import datetime

Now = datetime. datetime. now ()

Delta = datetime. timedelta (days = 3)

N_days = now + delta

Print n_days.strftime ('% Y-% m-% d % H: % M: % s ')

The advantage of datetime is that it can realize convenient time operations, such as endTime-starTime, which is very convenient in time duration calculation.

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.