7 examples of 'python datetime month and year only' in Python

Every line of 'python datetime month and year only' 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
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)
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

20@register.filter
21def year(date):
22 """
23 Gets the year (integer) from a date object.
24 Necessary to pipe through the to_date filter.
25 Example:
26 {{ some_timestamp|to_date|year }}
27
28 This doesn't work, unfortunately:
29 {{ some_timestamp|to_date.year }}
30 """
31 return date.year
236def _to_month_num(year, month, year_zero=1900):
237 return (year - year_zero) * 12 + month - 1
114def normalize_year(y, m, d):
115 """taking into account negative month and day values"""
116 if not (1 <= m <= 12):
117 y_plus = math.floor((m - 1) / 12)
118 y += y_plus
119 m -= y_plus * 12
120
121 if d <= 0:
122 d += max_days_in_month(m, y)
123 m -= 1
124 y, m, d = normalize_year(y, m, d)
125
126 else:
127 days_in_month = max_days_in_month(m, y)
128 if d > days_in_month:
129 m += 1
130 d -= days_in_month
131 y, m, d = normalize_year(y, m, d)
132
133 return y, m, d
149def daysInMonth(self, month):
150 """returns last day of the month as integer 28-31"""
151 if self.isLeapYear():
152 return _daysInMonthLeapYear[month - 1]
153 else:
154 return _daysInMonthNormal[month - 1]
82def _get_month_period(self):
83 """
84 Returns beginning and an end for a month period.
85 """
86 start = datetime.datetime(self.now.year, self.now.month, 1)
87
88 if self.now.month == 12:
89 end = datetime.datetime(self.now.year, self.now.month, 31, 23, 59, 59, 999999)
90 else:
91 end = datetime.datetime(self.now.year, self.now.month + 1, 1, 23, 59, 59, 999999) - datetime.timedelta(1)
92
93 return start.strftime(self.template), end.strftime(self.template)
1399def format_day_of_year(self, num):
1400 return self.format(self.get_day_of_year(), num)

Related snippets