Get date object for the first/last day of the current year

PythonDate

Python Problem Overview


I need to get date objects for the first and last day in the current year.

Currently I'm using this code which works fine, but I'm curious if there's a nicer way to do it; e.g. without having to specify the month/day manually.

from datetime import date
a = date(date.today().year, 1, 1)
b = date(date.today().year, 12, 31)

Python Solutions


Solution 1 - Python

The only real improvement that comes to mind is to give your variables more descriptive names than a and b.

Solution 2 - Python

from datetime import datetime

starting_day_of_current_year = datetime.now().date().replace(month=1, day=1)    
ending_day_of_current_year = datetime.now().date().replace(month=12, day=31)

Solution 3 - Python

There is nothing in the python library but there are external libraries that wrap this functionality up. For example, pandas has a timeseries library, with which you can do:

from datetime import date
from pandas.tseries import offsets

a = date.today() - offsets.YearBegin()
b = date.today() + offsets.YearEnd()

Whilst pandas is overkill if all you want is year begin and year end functionality, it also has support for a lot of other high level concepts such as business days, holiday calendars, month/quarter/year offsets: http://pandas.pydata.org/pandas-docs/stable/timeseries.html#dateoffset-objects

Solution 4 - Python

You'll have some funky stuff going on if you happen to be running this late on New Year's Eve and the two calls to today() cross the year boundary. It's safer to do this:

from datetime import date

epoch_year = date.today().year
year_start = date(epoch_year, 1, 1)
year_end = date(epoch_year, 12, 31)

Solution 5 - Python

import datetime
year = 2016
first_day_of_year = datetime.date.min.replace(year = year)
last_day_of_year = datetime.date.max.replace(year = year)
print(first_day_of_year, last_day_of_year)

duh.

What if we want to get the exact time the year begins or ends? Same thing. Replace datetime.date with datetime.datetime, and there you go, you got the first last day of year in datetime.datetime format.

To make this even fancier, wrap the whole thing in a function:

import datetime

def year_range(year, datetime_o = datetime.date):
    return (
        datetime_o.min.replace(year = year),
        datetime_o.max.replace(year = year)        
    )

print(year_range(2016, datetime.date))
print(year_range(2016, datetime.datetime))

Output:

(datetime.date(2016, 1, 1), datetime.date(2016, 12, 31))
(datetime.datetime(2016, 1, 1, 0, 0), datetime.datetime(2016, 12, 31, 23, 59, 59, 999999))

Solution 6 - Python

one genius way to find out first and last day of year is code below this code works well even for leap years

    first_day=datetime.date(year=i,month=1, day=1)
    first_day_of_next_year=first_day.replace(year=first_day.year+1,month=1, day=1)
    last_day=first_day_of_next_year-jdatetime.timedelta(days=1)


               

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionThiefMasterView Question on Stackoverflow
Solution 1 - PythonNPEView Answer on Stackoverflow
Solution 2 - PythonIbrohim ErmatovView Answer on Stackoverflow
Solution 3 - PythondanioView Answer on Stackoverflow
Solution 4 - PythonTrentonView Answer on Stackoverflow
Solution 5 - PythonoakaighView Answer on Stackoverflow
Solution 6 - Pythonkazem qanatiView Answer on Stackoverflow