10 examples of 'get month from date python' in Python

Every line of 'get month from 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
721def first_day_of_month(date):
722 ddays = int(date.strftime("%d")) - 1
723 delta = timedelta(days=ddays)
724 return date - delta
107def getDaysInMonth(self, mth, year):
108 days = 0
109 if mth in [1, 3, 5, 7, 8, 10, 12]:
110 days = 31
111 elif mth in [4, 6, 9, 11]:
112 days = 30
113 elif (mth == 2 and self.isLeapYear(year)):
114 days = 29
115 else:
116 days = 28
117 return days
37def _get_month(x):
38 return x.month
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)
236def _to_month_num(year, month, year_zero=1900):
237 return (year - year_zero) * 12 + month - 1
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]
9def get_current_month_date():
10 today = date.today()
11 datem = date(today.year, today.month, 1)
12 return datem
54def next_month(date):
55 """Compute the date at the beginning of the following month from the given date.
56
57 Args:
58 date: A datetime.date instance.
59 Returns:
60 A datetime.date instance, the first day of the month following 'date'.
61 """
62 # Compute the date at the beginning of the following month.
63 year = date.year
64 month = date.month + 1
65 if date.month == 12:
66 year += 1
67 month = 1
68 return datetime.date(year, month, 1)
6def dayOfMonth(M):
7 return (28 if (M == 2) else 31-(M-1)%7%2)
144def get_previous_month_number(mm, yy):
145 # type: (int, int) -> Tuple[int, int]
146 """
147 Given month number and year get previous month number and adjust year if we flow into previous year
148
149 Args:
150 mm (int): upto 2 digit month number, between 1 and 12
151 yy (int): any year
152
153 Returns:
154 tuple: tuple containing
155 int: previous month number after mm
156 int: adjusted year for the previous month number
157
158 Raises:
159 ValueError: when mm is not between 1 and 12
160
161 """
162 if 1 <= mm <= 12:
163 if mm == 1:
164 mm = 12
165 yy -= 1
166 else:
167 mm -= 1
168 else:
169 raise ValueError('mm should be between 1 and 12 inclusive')
170
171 return mm, yy

Related snippets