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

Every line of 'python get year from date' 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
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
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

134def get_ordinal_date(date):
135 # This is the only way I can get matplotlib to do the dates right.
136 return int(dates.date2num(get_date(date)))
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)
38def calc_easter_sunday(year):
39 a = year % 19
40 b = year // 100
41 c = year % 100
42 d = (19 * a + b - b // 4 - ((b - (b + 8) // 25 + 1) // 3) + 15) % 30
43 e = (32 + 2 * (b % 4) + 2 * (c // 4) - d - (c % 4)) % 7
44 f = d + e - 7 * ((a + 11 * d + 22 * e) // 451) + 114
45 month = f // 31
46 day = f % 31 + 1
47 return datetime.date(year, month, day)
75@property
76def year(self):
77 return self.date.year
22def get_financial_year(year):
23 return get_financial_year_start(year), get_financial_year_start(year + 1) - timedelta(microseconds=1)
36def get_date_year(self):
37 return self.date.strftime("%Y")
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)
54def gen_date(date, year=None):
55 """
56 Generates date in APA format (currently outputs only year).
57
58 :param date: input date, required
59 :type date: str
60 :param year: input year, default is None
61 :type year: str or None
62 :return: date annotation in APA format
63 :rtype: str
64 """
65
66 year = parse_year(year) or parse_year(date)
67 if year:
68 return ' (%s).' % year
69 return ''
721def first_day_of_month(date):
722 ddays = int(date.strftime("%d")) - 1
723 delta = timedelta(days=ddays)
724 return date - delta

Related snippets