Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get the current season using .NET? (Summer, Winter, etc...)

Tags:

c#

datetime

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.

like image 484
John Gietzen Avatar asked Oct 16 '09 18:10

John Gietzen


4 Answers

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
}
like image 104
riofly Avatar answered Nov 19 '22 20:11

riofly


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.

like image 32
Ðаn Avatar answered Nov 19 '22 21:11

Ðаn


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

like image 2
Foxfire Avatar answered Nov 19 '22 21:11

Foxfire


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.

like image 2
rasx Avatar answered Nov 19 '22 21:11

rasx