Python date addition and subtraction, python date addition and subtraction

Source: Internet
Author: User
Tags timedelta

Python date addition and subtraction, python date addition and subtraction

Some date operations:

Some operations on Date: 1 # convert date to string and get the date after the specified (or system date) n days -- @ Eillot 2 def dataTimeToString (dsNow = ReservationTime (). get ("workerDate"), dsDays = 1): 3 "4: param dsNow: indicates the date and time of today, in seconds 5: param dsDays: represents the date after n days (hours, minutes, And seconds remain unchanged), the default value is 6 "7 # convert workerDate string to date string => datetime 8 workerDateTo_Datetime = datetime. datetime. strptime (dsNow, '% Y-% m-% D') 9 delta = datetime. timedelta (dsDays) 10 ndays_after = workerDateTo_Datetime + delta11 return ndays_after.strftime ('% Y-% m-% D') 12 13 # It is add (addition) and sub (subtraction ), which for "starttime" and "endtime ". @ eillot14 def timeOperation (Operationstarttime, Operationendtime, flag = 0): 15 16 "17: param Operationstarttime: 18: param Operationendtime: 19: param flag: used to mark the addition and subtraction of time, the default value is 0, indicating 20 "21 order_starttime = datetime after 1 hour of time. datetime. strptime (Operationstarttime, '% H: % m') 22 order_endtime = datetime. datetime. strptime (Operationendtime, '% H: % m') 23 delta = datetime. timedelta (hours = 1) 24 # datetime type: 14:00:00 converted to str, and formatted and output 25 add_order_starttime = (order_starttime + delta) According to '% H: % M ). strftime ('% H: % m') 26 add_order_endtime = (order_endtime + delta ). strftime ('% H: % m') 27 if flag = # Return to starttime and endtime29 add_order_starttime = (order_starttime-delta) one hour ago ). strftime ('% H: % m') 30 add_order_endtime = (order_endtime-delta ). strftime ('% H: % m') 31 return add_order_starttime, add_order_endtime # The returned value type is tuple (tuples)

  

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.