imagine kitty magazine

Skip to main content
  • Categories

    • America
    • ASP.NET
    • Bible tags
    • C#
    • Christianity
    • Classic ASP
    • Firearms
    • Humor
    • LINQ
    • MVC
    • Programming
    • Random
    • Trackbacks
    • Web design
    • Web standards
    • Why I am the way I am
    • Wordpress
  • Links

    • ASP.NET Resources
    • Bible Dude
    • Cake Wrecks
    • Church Communications Pro
    • Creating Passionate Users
    • Dossy’s Blog
    • Gary Turner’s html & css workshop
    • id Projections
    • Information Pollination
    • SystemDotWeb
    • The Crazy Rants of Samantha Burns
    • The Nice Jewish Website
    • The Sneeze
    • Tyssen Design
  • Pages

    • Bible verse tags 2.0
    • Privacy Policy
    • Steps to building a proper web page
  • Info

    • home
    • Log in
    • contact mark
    • blogs that link here*
    • valid xhtml
    • valid css
    • valid rss2
    • rss feed
Sigarms P229

spam this

« SCOPE_IDENTITY() with LINQ
Adding links to all pages on a site using CSS »

Finding the current season using C#

Recently someone challenged me to write a server-side method for finding the current season and changing a style sheet accordingly.

The first step is deciding how to differentiate the seasons. I figured to use the day of the year so that astronomically Spring begins on March 21st, the 80th day of the year. Summer begins on the 172nd day, Autumn, the 266th and Winter the 355th. Of course, on a leap year add one day to each, 81, 173, 267 and 356.

Now to find the day of the year use the DayOfYear property. Also, use the IsLeapYear property to take a day away on leap year if the current day is after February 28th (the 59th day of the year). This is easily done by converting the bool IsLeapYear to an Int32 giving a 0 during non-leap years and 1 during leap year.
int doy = DateTime.Now.DayOfYear - Convert.ToInt32((DateTime.IsLeapYear(DateTime.Now.Year)) && DateTime.Now.DayOfYear > 59);

Then find the current season using a few nested ternary operators (I like ternary operators)
string currentSeason = String.Format("{0}.css",((doy < 80 || doy >= 355) ? "winter" : ((doy >= 80 && doy < 172) ? "spring" : ((doy >= 172 && doy < 266) ? "summer" : "fall"))));

I use this in the head of my MasterPage:
<link rel="Stylesheet" runat="server" id="seasonSheet" type="text/css" />

So all I have to do to add the correct style sheet for the current season is this:
seasonSheet.Attributes.Add("href",currentSeason);

Now a stylesheet named spring.css, summer.css, fall.css or winter.css will be added to your rendered MasterPage.

I hope you’ve found this helpful.

This entry was posted on 28Dec09 @ 1518 and is filed under ASP.NET, C#. You can follow any responses to this entry through the RSS 2.0 feed. You can this page, leave a response or trackback from your own site.

Leave a comment

Sponsored links

468x60-1 email address
Google
Custom Search

Amazon Wishlist

Hello, friends! I've decided to put my Amazon wishlist online. If you feel nice today you can purchase one of the items listed and it will be shipped to my door. My birthday is May 13th and I will gladly accept gifts for any Christian or Jewish holiday. Thank you for your support.

Site search and links

I'm a friend of Israel

234x60

Open Trackback Alliance Logo


Widgetize!
WWW is deprecated
no-WWW class B
free email addresses
*blogs that link here powered by Technorati

©2013 imaginekittymagazine