5

Is there a way to, given a date, retrieve the season of the year? For any place on the globe?

Is this based on time zone as well as hemisphere?

Note that, In the southern hemisphere that summer is still during the warm months.

EDIT:

To clarify, I am talking about the astronomical seasons.

3
  • 1
    How do you define Summer, Fall, Winter and Spring? Oct 16, 2009 at 18:19
  • 1
    Are you going to want other Branding periods like Christmas, Easter, Halloween etc??
    – Ian G
    Oct 16, 2009 at 18:35
  • 1
    Anyone what to comment on the downvote? This is an actual question. I know that .NET has support for timezones, I would figure that seasons should be similarly do-able. Oct 16, 2009 at 21:57

10 Answers 10

12

You can use this simple code:

private int getSeason(DateTime date) {
    float value = (float)date.Month + date.Day / 100f;  // <month>.<day(2 digit)>    
    if (value < 3.21 || value >= 12.22) return 3;   // Winter
    if (value < 6.21) return 0; // Spring
    if (value < 9.23) return 1; // Summer
    return 2;   // Autumn
}

To include the seasons in the Southern Hemisphere the code can become:

private int getSeason(DateTime date, bool ofSouthernHemisphere) {
    int hemisphereConst = (ofSouthernHemisphere ? 2 : 0);
    Func<int, int> getReturn = (northern) => {
        return (northern + hemisphereConst) % 4;
    };
    float value = (float)date.Month + date.Day / 100f;  // <month>.<day(2 digit)>
    if (value < 3.21 || value >= 12.22) return getReturn(3);    // 3: Winter
    if (value < 6.21) return getReturn(0);  // 0: Spring
    if (value < 9.23) return getReturn(1);  // 1: Summer
    return getReturn(2);     // 2: Autumn
}
9
  • The two vertical hemispheres have opposite seasons, so how will you account for that?
    – B.K.
    Aug 6, 2014 at 21:36
  • 1
    B.K., I changed the response, but I don't run any test to it.
    – riofly
    Aug 7, 2014 at 16:10
  • 1
    Change 100 to 100f, or that calculation will always yield a whole number.
    – B.K.
    Aug 7, 2014 at 21:42
  • 4
    This also fails to account for the the fact that summer solstice, fall equinox and winter solstice don't always have the same dates. You only manage to account for that in your winter if statement by a chance. Every leap year, summer solstice starts one day earlier (6.20, instead of 6.21), so your calculation would be off in 2016 on that part. Furthermore, every leap year and the year to follow, fall equinox starts one day earlier (9.22, instead of 9.23). Lastly, winter solstice starts on 12.21 every year except the year before the leap year -- it starts on 12.22 then.
    – B.K.
    Aug 7, 2014 at 22:10
  • 1
    I found more details of right seasons days. I look that it is more complex. Look this table: paolociraci.it/meteo/equinozio-solstizio.htm the first column is the start date of spring equinox, the second column is summer solstice, the third is autumn equinox, last is winter solstice. Do you think that your instructions may be a valid approximation?
    – riofly
    Aug 8, 2014 at 8:42
5

The answer depends on exactly how you want to define each season. This chart at Wikipedia shows the exact day and time changes slightly from year-to-year.

A simple solution that might be "good enough" is to use four fixed dates, say: 20-March, 21-June, 22-September, and 21-December.

2

I don't think this is standardized. Nor is this part of the well known globalization datasets.

1
  • I was looking for a globalized dataset. But, if there is none, I will just take what YOu have said and run with the US standard. Oct 17, 2009 at 22:47
2

Someone else can rattle off the code for you quickly but from the very Wikipedia.org article you referenced we have this:

The temperate areas

We can clearly distinguish six seasons. Dates listed here are for the Northern Hemisphere:[citation needed]

* Prevernal (1 March–1 May)
* Vernal (1 May–15 June)
* Estival (15 June–15 August)
* Serotinal (15 August–15 September)
* Autumnal (15 September–1 November)
* Hibernal (1 November–1 March)

You can then write a GetTemperateSeason() function to return the enumeration above based the month ranges.

2
public class Season
{
    private const string WINTER = "Winter";
    private const string SPRING = "Spring";
    private const string SUMMER = "Summer";
    private const string AUTUMN = "Autumn";

    public string Name { get; set; }
    public string Value { get; set; }

    public List<Season> LastFiveBillingQuarters
    {
        get
        {
            IList<Season> billingPeriods = new List<Season>();
            StringBuilder sbDisplayText;
            DateTime billingPeriod;

            for (int i = 0; i >= -12; i -= 3)
            {
                billingPeriod = DateTime.Now.AddMonths(i);
                var month = billingPeriod.Month;
                var day = billingPeriod.Day;
                var year = billingPeriod.Year;
                var ticks = billingPeriod.ToString();

                sbDisplayText = new StringBuilder();

                if ((month >= 12 || month < 03) & day >= 22)
                    sbDisplayText.Append(WINTER);
                else if (month >= 09 & day >= 23)
                    sbDisplayText.Append(AUTUMN);
                else if (month >= 06 & day >= 21)
                    sbDisplayText.Append(SUMMER);
                else if (month >= 03 & day >= 21)
                    sbDisplayText.Append(SPRING);

                sbDisplayText.Append(string.Format("{0}{1}", " ", year));

                billingPeriods.Add(new Season() { Name = sbDisplayText.ToString(), Value = ticks });
            }

            return billingPeriods.ToList();
        }
    }
}
0
1

A bit late (and only using norther hemisphere), but if someone else is stumbling upon this issue and wants a more accurate approximation you can use the following:

private int getSeason(DateTime date)
    {
        bool lastYearIsLeap = DateTime.IsLeapYear(date.Year-1);
        bool thisIsLeap = DateTime.IsLeapYear(date.Year);
        bool nextYearIsLeap = DateTime.IsLeapYear(date.Year+1);

        float summerStart = 6.21f;
        float autumnStart = 9.23f;
        float winterStart = 12.21f;

        //check if we need summer adjustment
        if (thisIsLeap)
        {
            summerStart = 6.20f;
        }
        //check if we need autumn adjustment
        if (thisIsLeap || lastYearIsLeap)
        {
            autumnStart = 9.22f;
        }
        //check if we need winter adjustment
        if (nextYearIsLeap)
        {
            winterStart = 12.22f;
        }

        if (date.Year == 2034 || date.Year == 2038)
            autumnStart -= 0.01f;

        float value = (float)date.Month + date.Day / 100f;   // <month>.<day(2 digit)>
        if (value < 3.20 || value >= winterStart) return 3;   // Winter
        if (value < summerStart) return 0; // Spring
        if (value < autumnStart) return 1; // Summer
        return 2;   // Autumn



    }

This is accurate until 2041. (would be accurate to 2034, but hard coded a fix for 2034 and 2038...assuming that http://www.paolociraci.it/meteo/equinozio-solstizio.htm is correct.

A helper method which is useful for checking the dates (in this case from 2010 -2050

 public static string PrintSeasons()
    {
        DateTime checkDateTime = new DateTime(2010, 1, 1);
        DateTime stopDateTime = new DateTime(2050, 1, 1);

        List<KeyValuePair<string, DateTime>> Seasons = new List<KeyValuePair<string, DateTime>>();

        while (checkDateTime < stopDateTime)
        {
            DateTime switchTime = checkDateTime;
            switch (getSeason(switchTime))
            {
                case 0: //Spring
                    if (switchTime.Month == 3 && switchTime.Day > 18 && switchTime.Day < 22)
                    {
                        KeyValuePair<string, DateTime> seasonEntry = new KeyValuePair<string, DateTime>("Spring", switchTime);
                        if (!Seasons.Any(o => o.Key == "Spring" && o.Value.Year == seasonEntry.Value.Year))
                            Seasons.Add(seasonEntry);
                    }
                    break;

                case 1: //Summer
                    if (switchTime.Month == 6 && switchTime.Day > 19 && switchTime.Day < 22)
                    {
                        KeyValuePair<string, DateTime> seasonEntry = new KeyValuePair<string, DateTime>("Summer", switchTime);
                        if (!Seasons.Any(o => o.Key == "Summer" && o.Value.Year == seasonEntry.Value.Year))
                            Seasons.Add(seasonEntry);
                    }
                    break;
                case 2: //Autumn
                    if (switchTime.Month == 9 && switchTime.Day >= 19 && switchTime.Day < 25)
                    {
                        KeyValuePair<string, DateTime> seasonEntry = new KeyValuePair<string, DateTime>("Autumn", switchTime);
                        if (!Seasons.Any(o => o.Key == "Autumn" && o.Value.Year == seasonEntry.Value.Year))
                            Seasons.Add(seasonEntry);
                    }
                    break;
                case 3: //Winter
                    if (switchTime.Month == 12 && switchTime.Day > 19 && switchTime.Day < 23)
                    {
                        KeyValuePair<string, DateTime> seasonEntry = new KeyValuePair<string, DateTime>("Winter", switchTime);
                        if (!Seasons.Any(o => o.Key == "Winter" && o.Value.Year == seasonEntry.Value.Year))
                            Seasons.Add(seasonEntry);
                    }
                    break;
            }

            checkDateTime = checkDateTime.AddDays(1);
        }

        DateTime currentYear = new DateTime(2000, 1, 1);
        string test = currentYear.Year + " | ";
        foreach (var season in Seasons)
        {
            if (currentYear.Year != season.Value.Year)
            {
                test += Environment.NewLine + season.Value.Year + " | ";
                currentYear = season.Value;
            }
            test += season.Key + ": " + season.Value.ToString("dd") + " | ";
        }

        return test;
    }
1

Asp.net has PersianCalendar in System.Globalization built-in, you can use it to obtain the seasons easily.

In persian calendar :

  • Months 1 to 3 = Spring
  • Months 4 to 6 = Summer
  • Months 7 to 9 = Autumn
  • Months 10 to 12 = Winter
enum Season
{
    Sprint = 1,
    Summer = 2,
    Autumn = 3,
    Winter = 4
};


var persianMonth = new PersianCalendar().GetMonth( DateTime.Now );

var season = (Season) Math.Ceiling( persianMonth / 3.0 )
0

Personally, unless it was directly required, I would describe the different sections by the quarter rather than climate/period of the year.

1
  • 4
    Then whoever is doing the marketing/branding ought to tell you when you should switch from Summer to Fall. Expecting somehow that a computer can figure this out is but a small step towards having HAL rule the world. Oct 16, 2009 at 18:35
0

northern/southern:

21/03 - start of spring/autumn

21/06 - start of summer/winter

23/09 - start of autumn/spring

22/12 - start of winter/summer

sometimes it IS delyed by one or two days, but for this you'll have to check in sites such as: timeanddate.com

2
  • There is no specific dates for the seasons, as they depend on the mean temperature.
    – Guffa
    Oct 16, 2009 at 19:14
  • Actually the date shifts based on the solstice and equinox and not the temperature. Mar 23, 2010 at 18:10
0

I needed to have a more flexible approach. My seasons are stored in the database, so I can't just compare day/month.

But if you try to think of an equation to compare the start/end date of a season to a specific day, it's not as easy as it seems since the newyear always appears to bite you in the neck. To get around this I decided to split the season that crosses the newyear into 2 seperate seasons. Then you can easily just compare day/month with math logic and determine the correct season.

Approach in C#

public class SeasonChecker
{
    public SeasonChecker()
    {
    }

    public Task<TSeason> FindSeasonAsync<TSeason>(IEnumerable<TSeason> seasons, DateTime date) where TSeason : class, ISeason
    {
        var result = seasons
            .Select(s =>
            {
                // Find season that crosses newyear
                if (s.Start.Year == s.End.Year)
                {
                    return new[] {
                        new {
                            OriginalSeason = s,
                            // Remap the season to the year 2000
                            ProcessableSeason = new InternalSeason {
                                Name = s.Name,
                                Start = new DateTime(2000, s.Start.Month, s.Start.Day),
                                End = new DateTime(2000, s.End.Month, s.End.Day)
                            }
                        }
                    };
                }
                else
                {
                    // If the season crosses the newyear, split the season
                    return new[] {
                        new {
                            OriginalSeason = s,
                            // Remap the season to the year 2000
                            ProcessableSeason = new InternalSeason {
                                Name = s.Name,
                                Start = new DateTime(2000, s.Start.Month, s.Start.Day),
                                End = new DateTime(2000, 12, 31)
                            }
                        },
                        new {
                            OriginalSeason = s,
                            // Remap the season to the year 2000
                            ProcessableSeason = new InternalSeason {
                                Name = s.Name,
                                Start = new DateTime(2000, 1, 1),
                                End = new DateTime(2000, s.End.Month, s.End.Day)
                            }
                        }
                    };
                }
            })
            .SelectMany(s => s)
            .FirstOrDefault(s =>
                // Now we can easily compare the dates.
                DateTime.Compare(s.ProcessableSeason.Start, new DateTime(2000, date.Month, date.Day)) <= 0 &&
                DateTime.Compare(new DateTime(2000, date.Month, date.Day), s.ProcessableSeason.End) <= 0
            )?.OriginalSeason;

        return Task.FromResult(result);
    }
}

public interface ISeason
{
    string Name { get; set; }
    DateTime Start { get; set; }
    DateTime End { get; set; }
}

internal class InternalSeason : ISeason
{
    public string Name { get; set; }
    public DateTime Start { get; set; }
    public DateTime End { get; set; }
}

This LINQ expression is entirely convertible to SQL, so you can apply it to a DbSet.

Approach in SQL

DECLARE @datum DATE;
SET @datum = '2020-12-21';

SELECT
    Season.SeasonId,
    Season.DateFrom,
    Season.DateUntil,
    CASE
        WHEN YEAR(Season.DateFrom) = YEAR(Season.DateUntil)
        THEN
            CASE
                WHEN
                    (
                        -- Season.DateFrom (2000) < NOW (2000)
                        MONTH(Season.DateFrom) < MONTH(CAST(@datum AS SQL_DATE))
                        OR (
                            MONTH(Season.DateFrom) = MONTH(CAST(@datum AS SQL_DATE))
                            AND
                            DAYOFMONTH(Season.DateFrom) <= DAYOFMONTH(CAST(@datum AS SQL_DATE))
                        )
                    )
                    AND
                    (
                        -- NOW (2000) < Season.DateUntil (2000)
                        MONTH(CAST(@datum AS SQL_DATE)) < MONTH(Season.DateUntil)
                        OR (
                            MONTH(CAST(@datum AS SQL_DATE)) = MONTH(Season.DateUntil)
                            AND
                            DAYOFMONTH(CAST(@datum AS SQL_DATE)) <= DAYOFMONTH(Season.DateUntil)
                        )
                    )
                THEN TRUE
                ELSE FALSE
            END
        ELSE
            CASE
                -- ( Season.DateFrom (2000) < NOW (2000) < 31/12) OR (1/1/2000 < NOW (2000) < Season.DateUntil (2000) )
                WHEN
                    -- Season.DateFrom (2000) < NOW (2000) < 31/12/2000)
                    (
                        (
                            -- Season.DateFrom (2000) <= NOW (2000)
                            MONTH(Season.DateFrom) < MONTH(CAST(@datum AS SQL_DATE))
                            OR (
                                MONTH(Season.DateFrom) = MONTH(CAST(@datum AS SQL_DATE))
                                AND
                                DAYOFMONTH(Season.DateFrom) <= DAYOFMONTH(CAST(@datum AS SQL_DATE))
                            )
                        )
                        AND
                        (
                            -- NOW (2000) <= 31/12)
                            MONTH(CAST(@datum AS SQL_DATE)) < 12
                            OR (
                                MONTH(CAST(@datum AS SQL_DATE)) = 12
                                AND
                                DAYOFMONTH(CAST(@datum AS SQL_DATE)) <= 31
                            )
                        )
                    )
                    OR
                    -- 1/1/2000 < NOW (2000) < Season.DateUntil (2000)
                    (
                        (
                            -- 1/1/2000 < NOW (2000)
                            1 < MONTH(CAST(@datum AS SQL_DATE))
                            OR (
                                1 = MONTH(CAST(@datum AS SQL_DATE))
                                AND
                                1 <= DAYOFMONTH(CAST(@datum AS SQL_DATE))
                            )
                        )
                        AND
                        (
                            -- NOW (2000) < Season.DateUntil (2000)
                            MONTH(CAST(@datum AS SQL_DATE)) < MONTH(Season.DateUntil)
                            OR (
                                MONTH(CAST(@datum AS SQL_DATE)) = MONTH(Season.DateUntil)
                                AND
                                DAYOFMONTH(CAST(@datum AS SQL_DATE)) <= DAYOFMONTH(Season.DateUntil)
                            )
                        )
                    )
                THEN TRUE
                ELSE FALSE
                END
        END
        AS CurrentSeason
FROM Season

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.