Python uses the datetime module to calculate various time interval methods, pythondatetime

Source: Internet
Author: User
Tags timedelta

Python uses the datetime module to calculate various time interval methods, pythondatetime

This example describes how to use the datetime module in python to calculate various time intervals. Share it with you for your reference. The specific analysis is as follows:

In python, The datetime module can be used to conveniently calculate the difference between two time points. The unit of datetime time difference can be days, hours, seconds, or even microseconds, the following code demonstrates the powerful functions of the datetime module in calculating the time difference.

#-*-Coding: UTF-8 -*-#! /Usr/bin/env pythonimport datetime # Calculate the normal time of datetime in d1 = datetime. datetime (2013, 8, 15, 50) d2 = datetime. datetime (2013, 8, 9, 0) d3 = datetime. timedelta (microseconds = 5000) print U' difference: % s microsecond '% (d1-d2 ). microsecondsprint U' difference: % s second '% (d1-d2 ). secondsprint U' difference: % s day '% (d1-d2 ). daysprint U' time interval: % s microsecond '% d3 # Time Zone conversion. The current system is located in the time zone + 1d = datetime. datetime. now () d = d + datetime. timedelta (seconds = 3600) print dprint d. ctime ()

The output result is as follows:

Difference: 0 microseconds difference: 67260 seconds difference: 0 days interval: 0: 00: 00.005000 microseconds 11:29:29. 663000Fri Aug 30 11:29:29 2013

I hope this article will help you with 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.