10 examples of 'python date difference in days' in Python

Every line of 'python date difference in days' 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
this disclaimer
26def days_ago(days):
27 """
28 Returns datetime object
29 """
30 return datetime.date.today() - datetime.timedelta(days=days)
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

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)
41def age_in_days(date):
42 return (datetime.now() - date).days
157def _days_since(data_str: str) -> int:
158 """
159 Return number of days since the datetime passed as input in the form '%Y-%m-%d %H:%M:%S'.
160
161 This can be used to calculate how many days an update is in current state by passing
162 directly the 'date_pushed' or 'date_submitted' from the Update object.
163 This is also useful to easily mock the output, since datetime.datetime.utcnow()
164 cannot be mocked.
165
166 Args:
167 data_str: The 'date_pushed' or 'date_submitted' from the Update object.
168
169 Returns:
170 Number of days since the date in input.
171 """
172 update_time = datetime.datetime.strptime(data_str, '%Y-%m-%d %H:%M:%S')
173 return (datetime.datetime.utcnow() - update_time).days
29def to_days_since_epoch(d):
30 return (d - datetime.datetime(1970, 1, 1)).days
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)
22def age(bday, d=None):
23 """ http://www.djangosnippets.org/snippets/557/ """
24 if d is None:
25 d = datetime.date.today()
26 return (d.year - bday.year) - int((d.month, d.day) < (bday.month, bday.day))
721def first_day_of_month(date):
722 ddays = int(date.strftime("%d")) - 1
723 delta = timedelta(days=ddays)
724 return date - delta
244def days_until(date):
245 """Calculate days until."""
246 show_date = dt_util.as_local(date)
247 now = dt_util.as_local(dt_util.now())
248 return int((show_date - now).total_seconds() / 86400)
56def _increment_day(date, i):
57 # increment date by i days
58 return date + datetime.timedelta(days=i)

Related snippets