10 examples of 'add days to date python' in Python

Every line of 'add days to date python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
668def add_years(d, years):
669 """
670 Add 'year' years to the date 'd'.
671
672 Return a date that's `years` years after the date (or datetime)
673 object `d`. Return the same calendar date (month and day) in the
674 destination year, if it exists, otherwise use the following day
675 (thus changing February 29 to March 1).
676 source: https://stackoverflow.com/a/15743908/1518684
677
678 Parameters:
679 ----------
680 d: datetime.date
681 The date to which 'years' years should be added.
682 years: int
683 The number of years to add to date.
684
685 Returns:
686 ----------
687 new_d: datetime.date
688 Date d incremented by 'years' years.
689
690 """
691 try:
692 return d.replace(year = d.year + years)
693 except ValueError:
694 return d + (date(d.year + years, 1, 1) - date(d.year, 1, 1))
40def add_days(self, days):
41 self._days += days
69def add_month_based_on_weekday(date):
70 # Is the weekday of this date the last one?
71 is_last_weekday = (date + datetime.timedelta(weeks=1)).month != date.month
72
73 # Some magic which pushes and pulls some weeks until
74 # it fits right.
75 new_date = date + datetime.timedelta(weeks=4)
76 if (new_date.day + 6) // 7 < (date.day + 6) // 7:
77 new_date += datetime.timedelta(weeks=1)
78 next_month = add_month(date, override_day=1)
79 if new_date.month == (next_month.month - 1 if next_month.month > 1 else 12):
80 new_date += datetime.timedelta(weeks=1)
81 elif new_date.month == (next_month.month + 1 if next_month.month < 12 else 1):
82 new_date += datetime.timedelta(weeks=-1)
83
84 # If the weekdate of the original date was the last one,
85 # and there is some room left, add a week extra so this
86 # will result in a last weekday of the month again.
87 if is_last_weekday and (new_date + datetime.timedelta(weeks=1)).month == new_date.month:
88 new_date += datetime.timedelta(weeks=1)
89
90 return new_date
9def days_since_1900(date):
10 """Convert string date to days since 1/1/1900
11 Intakes a date month/day/year and returns number of days since 1/1/1900
12 convert_date('1/1/1950') -> 18262. Note 1900 was not leap year
13 """
14
15 days_in_month = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
16 date_array = date.split('-')
17 month = int(date_array[1])
18 day = int(date_array[2])
19 year = int(date_array[0]) - 1900 #looking for number of days since 1900
20
21# Start with days in previous years
22 days_prev_years = year * 365
23
24# Add the appropriate leap years
25 leap_years = int(year/4)
26 if (year%4 == 0 and month < 3):
27 leap_years = leap_years - 1
28 days_prev_years += leap_years
29
30# Now count up the days this year, start with days in previous months
31 days_this_year = 0
32 for imonth in range(0,month-1):
33 days_this_year += days_in_month[imonth]
34
35# Add previous days this month
36 days_this_year += day-1
37
38 days = days_prev_years + days_this_year
39 return(days)
517def daysbetween(day1, month1, year1, day2, month2, year2):
518 """Given two dates it returns the number of days between them.
519 If date1 is earlier than date2 then the result will be positive."""
520 return daycount(year2, month2, day2) - daycount(year1, month1, day1)
30def get_todays_date():
31 return timezone.now().astimezone(PACIFIC_TIME).date()
12def add_days_delta(date_from, days_delta):
13 if not date_from:
14 return date_from
15 next_date = fields.Date.from_string(date_from) + timedelta(days=days_delta)
16 return fields.Date.to_string(next_date)
73def python_to_ABAP_date(py_date):
74 return "{:04d}{:02d}{:02d}".format(py_date.year, py_date.month, py_date.day)
86def add_day(x):
87 if x not in self.weekdays:
88 self.weekdays.append(x)
71def toDate(mon, yr):
72 #Parse month
73 newMon = monthMapper(mon)
74
75 #Parse day
76 newDay = 1
77
78 #Parse year
79 if yr < 50:
80 yr += 2000
81 elif yr <= 99:
82 yr += 1900
83 newYr = yr
84
85 return date(newYr, int(newMon), newDay)

Related snippets