Python uses the datetime module to compute the various time interval methods _python

Source: Internet
Author: User
Tags datetime time interval timedelta

The examples in this article describe how Python calculates various time intervals using a datetime module. Share to everyone for your reference. The specific analysis is as follows:

Python through the datetime module can be very convenient to calculate the difference of two times, datetime units can be days, hours, seconds, or even microseconds, the following code shows the DateTime module in the calculation of the power of time lag

#-*-Coding:utf-8-*-
#!/usr/bin/env python
import datetime
#datetime一般的时间计算
d1 = datetime.datetime ( 2013, 8, 05,15,50)
D2 = Datetime.datetime (2013, 8, 4,21,9,0,0)
d3 = Datetime.timedelta (microseconds=5000
) Print u ' difference:%s microseconds '% (D1-D2). microseconds
print u ' difference:%s seconds '% (d1-d2). Seconds
Print u ' difference:%s days '% (D1-D2).
Day Print U ' interval:%s microsecond '%d3
#时区转换, current system in time zone +1
d = datetime.datetime.now ()
d = d + Datetime.timedelta (seconds= 3600)
Print D
print d.ctime ()

The output results are as follows:

Difference: 0 microseconds
difference: 67,260 seconds
difference: 0 days
interval: 0:00:00.005000 microsecond
2013-08-30 11:29:29.663000
Fri Aug 30 11:29:29 2013

I hope this article will help you with your Python programming.

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.