ASP.NET DateTime


8/24/2009 3:48:15 PM

The ASP.NET DateTime can be used in many different ways. One of the recent things I had to use it for was making sure that only content after a certain date would be published. So, what I had to do was make sure that today's date was greater than the 24th. If it was I had to compile 3 strings to make a title. If it was not I had to retrieve the title from the database.

This was my approach:

I have a label in my mark-up.

And in code behind I want to:

Store the date I wanted to compare with in a DateTime variable.

 DateTime goLiveDate = new DateTime("dd/MM/yy"); 

Check if todays date is greater (or after) the goLiveDate

 DateTime goLiveDate = new DateTime("dd/MM/yy");  If (DateTime.Now > goLiveDate) {  } 

If it is then I want to set a label to the appropriate text

 DateTime goLiveDate = new DateTime("dd/MM/yy");  If (DateTime.Now > goLiveDate) { 	headerLabel.Text = "This is after go live."; } 

Anything else must be before go live date

 DateTime goLiveDate = new DateTime("dd/MM/yy");  If (DateTime.Now > goLiveDate) { 	headerLabel.Text = "This is after go live."; } else { 	headerLabel.Text = "This is before go live."; }