Python Study Notes 15: Date and Time Processing notes, python Study Notes

Source: Internet
Author: User
Tags timedelta

Python Study Notes 15: Date and Time Processing notes, python Study Notes

#-*-Coding: UTF-8-*-import datetime # date def dateadd_day (days) for the given date to the next N days: d1 = datetime. datetime. now () d3 = d1 + datetime. timedelta (days) return d3 # Yesterday def getYesterday (): today = datetime. date. today () oneday = datetime. timedelta (days = 1) yesterday = today-oneday return yesterday # today def getToday (): return datetime. date. today () # Get the date of the previous day of the given parameter, and return a listdef getDaysBefore (num): today = datetime. date. today () Oneday = datetime. timedelta (days = 1) li = [] for I in range (0, num): # Today minus one day, one day minus today = today-oneday # convert the date to the string li. append (datetostr (today) return li # convert string to datetime Type def strtodatetime (datestr, format): return datetime. datetime. strptime (datestr, format) # convert time to a string in the format of 2015-02-02def datetostr (date): return str (date) [0: 10] # convert time to a string, format: 2015-02-02def datetostr_secod (date): return str (date) [0: 19] # Two date phases How many days are there, for example, 2015-2-04 and 2015-3-1def datediff (beginDate, endDate): format = "% Y-% m-% d" bd = strtodatetime (beginDate, format) ed = strtodatetime (endDate, format) oneday = datetime. timedelta (days = 1) count = 0 while bd! = Ed: ed = ed-oneday count + = 1 return count # Second def datediff_seconds (beginDate, endDate) for the difference between two dates ): format = "% Y-% m-% d % H: % M: % S" if "" not in beginDate or ':' not in beginDate: bformat = "% Y-% m-% d" else: bformat = format if "" not in endDate or ':' not in endDate: eformat = "% Y-% m-% d" else: eformat = format starttime = strtodatetime (beginDate, bformat) endtime = strtodatetime (endDate, eformat) ret = endtime-starttime return ret. days * 86400 + ret. seconds # Get all time in two time periods and return listdef getDays (beginDate, endDate): format = "% Y-% m-% d" begin = strtodatetime (beginDate, format) oneday = datetime. timedelta (days = 1) num = datediff (beginDate, endDate) + 1 li = [] for I in range (0, num): li. append (datetostr (begin) begin = begin + oneday return li # obtain the current year as a string def getYear (date = datetime. date. today (): return str (date) [0: 4] # obtain the current month as a string def getMonth (date = datetime. date. today (): return str (date) [] # obtain the current day as a string def getDay (date = datetime. date. today (): return str (date) [8: 10] # obtain the current hour as a string def getHour (date = datetime. datetime. now (): return str (date) [11: 13] # obtain the current minute as a string def getMinute (date = datetime. datetime. now (): return str (date) [] # obtain the current second as a string def getSecond (date = datetime. datetime. now (): return str (date) [17:19] def getNow (): return datetime. datetime. now () print dateadd_day (10) #16:41:13. 275000 print getYesterday () # 2015-02-03print getToday () # 2015-02-04print getDaysBefore (3) #['2017-02-03 ', '2017-02-02 ', '2017-02-01 '] print datediff ('2017-2-01', '2017-10-05 ') #2015 print datediff_seconds ('2017-02-04', '2017-02-05 ') #86400 print datediff_seconds ('2017-02-04 22:00:00 ', '2017-02-05') #2015 print getDays ('2017-2-03 ', '2017-2-05 ') #['2017-02-03 ', '2017-02-04', '2017-02-05 '] print datetostr_secod (getNow () #2015-02-04 16: 46: 47 print str (getYear (dateadd_day (-50) + '-' \ + getMonth () + '-' \ + getDay () + ''\ + getHour () + ':' \ + getMinute () + ':' \ + getSecond () #16: 59: 04 print getNow () #16:46:47. 454000

Backup

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.