Where season do we live?
When I was developing a website, we’ve a little problem. We’ve to personalize the header with the season where we live.
I thought some ways to solve this and finally I decided to inherit the webcontrol System.Web.UI.WebControls.Image and inside the event OnLoad change the image to show. But I ned a function that it isn’t inside .NET, to know the season. For it I wrote the following enum and function:
public enum Season
{
Winter,
Spring,
Summer,
Automn
}
public static Season getSeason(DateTime date)
{
Season season = null;
//Winter
if (date.Month >= 1 & date.Month <= 3)
{
switch (date.Month)
{
case 3:
if (date.Day <= 20)
{
season = Season.Winter;
}
break;
default:
season = Season.Winter;
break;
}
}
//Spring
if (date.Month >= 3 & date.Month <= 6)
{
switch (date.Month)
{
case 3:
if (date.Day > 20)
{
season = Season.Spring;
}
break;
case 6:
if (date.Day <= 20)
{
season = Season.Spring;
}
break;
default:
season = Season.Spring;
break;
}
}
//Summer
if (date.Month >= 6 & date.Month <= 9)
{
switch (date.Month)
{
case 6:
if (date.Day > 20)
{
season = Season.Summer;
}
break;
case 9:
if (date.Day <= 20)
{
season = Season.Summer;
}
break;
default:
season = Season.Summer;
break;
}
}
//Autumn
if (date.Month >= 9 & date.Month <= 12)
{
switch (date.Month)
{
case 9:
if (date.Day > 20)
{
season = Season.Automn;
}
break;
case 12:
if (date.Day <= 20)
{
season = Season.Automn;
}
else
{
season = Season.Winter;
}
break;
default:
season = Season.Automn;
break;
}
}
return season;
}
Now, we only have to override the OnLoad event in the constructor and select the correct image:
public SeasonsRotator()
{
this.Load += new EventHandler(SeasonsRotator_Load);
}
void SeasonsRotator_Load(object sender, EventArgs e)
{
switch (getSeason(DateTime.Now))
{
case Season.Winter:
this.ImageUrl = "~/winter.jpg";
break;
case Season.Spring:
this.ImageUrl = "~/spring.jpg";
break;
case Season.Summer:
this.ImageUrl = "~/summer.jpg";
break;
case Season.Automn:
this.ImageUrl = "~/automn.jpg";
break;
default:
break;
}
}