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

匿名 (未验证) 提交于 2019-12-03 03:02:02

问题:

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.

回答1:

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



回答2:

You can use this simple code:

private int getSeason(DateTime date) {     float value = (float)date.Month + date.Day / 100;   // <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 } 


回答3:

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.



回答4:

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]

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



回答5:

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();         }     } } 


回答6:

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



回答7:

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!