Python gets detailed explanations of time instances with a specified time difference, and python Time Difference

Source: Internet
Author: User

Python gets detailed explanations of time instances with a specified time difference, and python Time Difference

How to obtain a time instance with a specified time difference in python

Data that needs to be analyzed within a certain period of time, such as data required by the time within three days, two hours ago, is often intercepted. Therefore, this part of frequently used functions are modularized, this facilitates reuse later. I would like to share this with you.

Import time import sys reload (sys) def get_day_of_day (UTC = False, days = 0, hours = 0, miutes = 0, seconds = 0 ): ''''' if days> = 0, date is larger than today if days <0, date is less than today date format = "YYYY-MM-DD" ''' now = time. time () timeNew = now + days * 24*60*60 + hours * 60*60 + miutes * 60 + seconds if UTC: timeNew = timeNew + time. timezone t = time. localtime (timeNew) return time. strftime ('% Y-% m-% d % H: % M: % s', t) # Use UTC time two hours ago t = get_day_of_day (True, 0, -2) print t #3 days before local time t = get_day_of_day (False,-3) print t #3 days after local time t = get_day_of_day (False, 3) print t

Result After running:

20:25:56 20:25:56

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.