Find the Season from a Timestamp in Python: Exploring 2 Methods

Datetime Season

In this article, lets us understand how one can determine the season that is – winter, spring, summer, and autumn from provided timestamp or the date in Python programming language using the datetime module and the date built-in class of the same module. The date class is utilized for date formats, presuming the Gregorian calendar is currently in use. The following is the order of the attributes: Year, month, and day. Using the datetime module, users might very well work with a variety of classes that seamlessly works on the date as well as time data inputs and can integrate both date and time.

Methods to Determine Season from Timestamp in Python

Let us look at two different approaches both with their own pros and cons to determine the season for the provided date.

1. Using Simple Mathematical Formula to Determine Season

Simple maths is used in this approach, only the month is required to determine the season. This is quite a straightforward way, one cannot simply modify the seasons associated with the month. To do so new mathematical formula is required. The advantage of this method is that it is easy to implement, with no need to pre-define anything, just one line mathematical formula month % 12 // 3 + 1 is used.

#import required modules and packages
import datetime
from datetime import date

#defining function
def get_season(date):
  m = date.month
  x = m%12 // 3 + 1
  if x == 1:
    season = print("Winter")
  if x == 2:
    season = print("Spring")
  if x == 3:
    season = print("Summer")
  if x == 4:
    season = print("Autumn")
  return season

x = get_season(date(2023,12,1))
print(x, "-->", get_season(x))

Pros and Cons of this approach

  • This method is suitable when we are considering the standard four-season spring from March to May, summer from June to August, Autumn from September to November, and Winter from December to February.
  • The drawback of using this method is that if we consider seasons according to another hemisphere, for example in India the seasons described are three- Summer, Monsoon, and Winter, using this same mathematical formula will not work, one has to come up with different maths every time for every different situation which might not be very efficient at times.


Determine Season 1
Determine Season Approach – 1

2. Using Combined Month and Day Integer Values to Determine Season

To determine the season the year is not necessarily required. One needs to know the date: month and day, and that’s enough. In this approach, we are converting the month and day into a single integer by combining both values for example 3/20 is march 20th we will transform it into 3+ 20 = 320 similarly 12/10 is December 10th will get converted into 12 + 10 = 1210. Next, we define a function to determine the season using these combined values. Here in this example, four seasons are set.

#importing required modules
import datetime
from datetime import date

#defining function
def get_season(date):

    month = date.month * 100
    day =
    month_day = month + day  #combining month and day

    if ((month_day >= 301) and (month_day <= 531)):
        season = "Spring"
    elif ((month_day > 531) and (month_day < 901)):
        season = "Summer"
    elif ((month_day >= 901) and (month_day <= 1130)):
        season = "Autumn"
    elif ((month_day > 1130) and (month_day <= 229)):
        season = "Winter"
        raise IndexError("Invalid Input")

    return season

#calling the function
x =
print(x, "-->", get_season(x))

Pros and Cons of this approach

  • The major benefit of using this method is that one can modify the ‘if’ and ‘elif’ statements according to their requirements and can set seasons differently. The time period of seasons is to be set by the programmer and hence customization is possible in this approach.
  • The only drawback here is that one to pre-define the seasons in the function.


Determine Season 2
Determine Season Approach – 2

To understand the difference between the ‘/’ and ‘//’ operators in the python programming language, please click here.

Comparing Both Approaches and Final Thoughts

The datetime module in the python programming language is a great help when it comes to date and time data inputs. The module has many built-in classes which can be combined and used in efficient ways. In this article, we studied two approaches to determine the season from provided input date using the datetime module and the “date” package of the same module.


Official Document