Python module datetime

Source: Internet
Author: User
Tags instance method timedelta

1. Date output formatted datetime/= string

Import datetime

now = Datetime.datetime.now ()

Now.strftime ('%y-%m-%d%h:%m:%s ')

Output

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

Strftime is an instance method of a DateTime class.

2. Date Output Formatted string = DateTime

Import datetime

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

D = datetime.datetime.strptime (T_str, '%y-%m-%d%h:%m:%s ')

Strptime is a static method of a DateTime class.

3. Date comparison operation

There is a Timedelta class in the DateTime module, where objects of this class are used to represent a time interval, such as a difference of two dates or times.

Construction Method:

Import datetime

Datetime.timedelta (days=0, seconds=0, Microseconds=0, Milliseconds=0, Minutes=0, hours=0, weeks=0)

All parameters have a default value of 0, which can be either int or float, positive or negative.

The corresponding time value can be obtained by timedelta.days, Tiemdelta.seconds and so on.

An instance of the Timedelta class, which supports addition, subtraction, multiplication, and other operations, is also an example of the Timedelta class. Like what:

Import datetime

Year = Datetime.timedelta (days=365)

Ten_years = year *10

Nine_years = Ten_years-year

The date, time, and DateTime classes also support the addition and subtraction of Timedelta.

datetime1 = datetime2 + Timedelta

Timedelta = datetime1-datetime2

In this way, it is convenient to implement some functions.

4. The number of days between two dates.

Import datetime

D1 = Datetime.datetime.strptime (' 2015-03-05 17:41:20 ', '%y-%m-%d%h:%m:%s ')

D2 = Datetime.datetime.strptime (' 2015-03-02 17:41:20 ', '%y-%m-%d%h:%m:%s ')

Delta = d1-d2

Print Delta.days

Output: 3

5. Today's date of N days.

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:

2015-04-10 19:16:34

#coding =utf-8

Import datetime

Now=datetime.datetime.now ()

Print now

#将日期转化为字符串

DateTime-= string

Import datetime

Now=datetime.datetime.now ()

Print Now.strftime ('%y-%m-%d%h:%m:%s ')

#将字符串转换为日期string = DateTime

Import datetime

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

D=datetime.datetime.strptime (T_str, '%y-%m-%d%h:%m:%s ')

Print D

#在datetime模块中有timedelta类, the object of this class is used to represent a time interval, such as a two-day # period or a time difference.

#计算两个日期的间隔

Import datetime

D1 = Datetime.datetime.strptime (' 2012-03-05 17:41:20 ', '%y-%m-%d%h:%m:%s ')

D2 = Datetime.datetime.strptime (' 2012-03-02 17:41:20 ', '%y-%m-%d%h:%m:%s ')

Delta = d1-d2

Print delta.days Print Delta

#今天的n天后的日期

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 benefit of datetime is that convenient time operations such as endtime-startime can be achieved, which is very convenient when the time duration is calculated.

Transferred from: http://www.cnblogs.com/emanlee/p/4399147.html

Python module datetime

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.