A Few Date Methods
Posted by Ryan Baxter Tue, 06 May 2008 18:45:00 GMT
An ASP.NET project of mine recently required the calculation of the start and end date of the current date’s previous month. This was more difficult putting into words than code. It did, however, get me thinking about other common date routines.
public static DateTime FirstDayOfPreviousMonth
{
get
{
return new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddMonths(-1);
}
}
public static DateTime LastDayOfPreviousMonth
{
get
{
return new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1).AddDays(-1);
}
}Being preemptive, I decided to include a few methods for determining the start and end date of the current date’s fiscal quarter. I started by calculating the current date’s quarter. This was accomplished with just a little division.
public static int CurrentQuarter
{
get
{
return (DateTime.Now.Month + 2) / 3;
}
}Finding the start and end dates of the current quarter was harder, but could still be expressed in a single line of code (or two).
public static DateTime FirstDayOfCurrentQuarter
{
get
{
return new DateTime(DateTime.Now.Year, 1 + ((CurrentQuarter - 1) * 3), 1);
}
}
public static DateTime LastDayOfCurrentQuarter
{
get
{
int lastMonthOfCurrentQuarter = 3 + ((CurrentQuarter - 1) * 3);
return new DateTime(DateTime.Now.Year, lastMonthOfCurrentQuarter, DateTime.DaysInMonth(DateTime.Now.Year, lastMonthOfCurrentQuarter));
}
}I’d like to extend this collection to include other common date routines. Feel free to post your date methods as comments if you’d like to share.
*The methods I’ve created for calculating the current quarter’s start and end dates assume the fiscal year starts on January 1st. This may not be suitable for your needs.
- Posted in Code Snippets
- Meta no comments, permalink, rss, atom
System.Drawing.Color Hex Values
Posted by Ryan Baxter Wed, 16 Apr 2008 23:29:00 GMT
Do you know the hexidecimal value of PapayaWhip? How about BlanchedAlmond? Me either. I’m not a big fan of the System.Drawing.Color structure in the Microsoft .NET Framework. I don’t think the addition of color names makes good framework design sense. Unless you’re an Interior Designer; the majority of these colors won’t make sense to you either.
I’ve recently spent time reworking a few design elements of an ASP.NET website. All design related ASP.NET attributes were replaced with Cascading Style Sheets (CSS) where applicable. Unfortunately the website was spattered with named colors from the System.Drawing.Color structure. To help replace these named colors with hexadecimal values, I found the following resource extremely helpful.
http://www.opinionatedgeek.com/DotNet/Tools/Colors/default.aspx
- Posted in Expect the Unexpected
- Meta no trackbacks, no comments, permalink, rss, atom
Expect the Unexpected: Source Fource
Posted by Ryan Baxter Wed, 20 Feb 2008 16:09:00 GMT
OK, this is ridiculous. I must be getting too old for their marketing demographic. I do, however, look remarkably similar to figure below when wearing my dobak.

- Posted in Expect the Unexpected
- Meta 1 comment, permalink, rss, atom

