<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>crunchlife: A Few Date Methods</title>
    <link>http://crunchlife.com/articles/2008/05/06/a-few-date-methods</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description></description>
    <item>
      <title>A Few Date Methods</title>
      <description>&lt;p&gt;An ASP.NET project of mine recently required the calculation of the start and end date of the current date&amp;#8217;s previous month. This was more difficult putting into words than code.  It did, however, get me thinking about other common date routines.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;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);
    }
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Being preemptive, I decided to include a few methods for determining the start and end date of the current date&amp;#8217;s fiscal quarter. I started by calculating the current date&amp;#8217;s quarter. This was accomplished with just a little division.&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;public static int CurrentQuarter
{
    get
    {
        return (DateTime.Now.Month + 2) / 3;
    }
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;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).&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;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));
    }
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;I&amp;#8217;d like to extend this collection to include other common date routines. Feel free to post your date methods as comments if you&amp;#8217;d like to share.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;*The methods I&amp;#8217;ve created for calculating the current quarter&amp;#8217;s start and end dates assume the fiscal year starts on January 1st. This may not be suitable for your needs.&lt;/strong&gt;&lt;/p&gt;</description>
      <pubDate>Tue, 06 May 2008 11:45:00 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:0f4f646c-7a7d-42c8-b6b1-306d40200836</guid>
      <author>Ryan Baxter</author>
      <link>http://crunchlife.com/articles/2008/05/06/a-few-date-methods</link>
      <category>Code Snippets</category>
      <category>dotNET</category>
      <category>CSharp</category>
      <enclosure url="http://crunchlife.com/files/clock.jpg" type="image/jpeg" length="22591"/>
    </item>
  </channel>
</rss>
