<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Reader challenge!</title>
	<atom:link href="http://quickduck.com/blog/2008/11/03/reader-challenge/feed/" rel="self" type="application/rss+xml" />
	<link>http://quickduck.com/blog/2008/11/03/reader-challenge/</link>
	<description>Straight from the mind of geniuseseses....</description>
	<lastBuildDate>Thu, 29 Jul 2010 06:56:25 -0700</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<item>
		<title>By: Paulo Santos</title>
		<link>http://quickduck.com/blog/2008/11/03/reader-challenge/comment-page-1/#comment-2642</link>
		<dc:creator>Paulo Santos</dc:creator>
		<pubDate>Wed, 18 Mar 2009 14:33:13 +0000</pubDate>
		<guid isPermaLink="false">http://quickduck.com/blog/2008/11/03/reader-challenge/#comment-2642</guid>
		<description>ops, there&#039;s copy paste bug there.

DateTime beginningOfWeekDate1 = d1.AddDays(-(int)d1.DayOfWeek);</description>
		<content:encoded><![CDATA[<p>ops, there&#8217;s copy paste bug there.</p>
<p>DateTime beginningOfWeekDate1 = d1.AddDays(-(int)d1.DayOfWeek);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paulo Santos</title>
		<link>http://quickduck.com/blog/2008/11/03/reader-challenge/comment-page-1/#comment-2641</link>
		<dc:creator>Paulo Santos</dc:creator>
		<pubDate>Wed, 18 Mar 2009 14:32:24 +0000</pubDate>
		<guid isPermaLink="false">http://quickduck.com/blog/2008/11/03/reader-challenge/#comment-2641</guid>
		<description>This should work

        public static bool InSameWeek(DateTime d1, DateTime d2)
        {
            DateTime beginningOfWeekDate1 = d2.AddDays(-(int)d2.DayOfWeek);
            DateTime beginningOfWeekDate2 = d2.AddDays(-(int)d2.DayOfWeek);
            return beginningOfWeekDate1 == beginningOfWeekDate2;
        }


Thanks Ben!</description>
		<content:encoded><![CDATA[<p>This should work</p>
<p>        public static bool InSameWeek(DateTime d1, DateTime d2)<br />
        {<br />
            DateTime beginningOfWeekDate1 = d2.AddDays(-(int)d2.DayOfWeek);<br />
            DateTime beginningOfWeekDate2 = d2.AddDays(-(int)d2.DayOfWeek);<br />
            return beginningOfWeekDate1 == beginningOfWeekDate2;<br />
        }</p>
<p>Thanks Ben!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Paulo Santos</title>
		<link>http://quickduck.com/blog/2008/11/03/reader-challenge/comment-page-1/#comment-2640</link>
		<dc:creator>Paulo Santos</dc:creator>
		<pubDate>Wed, 18 Mar 2009 14:29:27 +0000</pubDate>
		<guid isPermaLink="false">http://quickduck.com/blog/2008/11/03/reader-challenge/#comment-2640</guid>
		<description>I think Andrew is far from being correct. Dates in different years will result in same week.
And what happens on cases where the 1st of january is a wednesday and you compare 30/Dec with 2/Jan?

I have now the same problem, I think I ll take a look at Ben&#039;s solution.</description>
		<content:encoded><![CDATA[<p>I think Andrew is far from being correct. Dates in different years will result in same week.<br />
And what happens on cases where the 1st of january is a wednesday and you compare 30/Dec with 2/Jan?</p>
<p>I have now the same problem, I think I ll take a look at Ben&#8217;s solution.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://quickduck.com/blog/2008/11/03/reader-challenge/comment-page-1/#comment-1644</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Tue, 04 Nov 2008 22:42:20 +0000</pubDate>
		<guid isPermaLink="false">http://quickduck.com/blog/2008/11/03/reader-challenge/#comment-1644</guid>
		<description>We should do more of these challenges as I find I always learn something new. 

Thanks Andrew for pointing me towards the GregorianCalendar class.</description>
		<content:encoded><![CDATA[<p>We should do more of these challenges as I find I always learn something new. </p>
<p>Thanks Andrew for pointing me towards the GregorianCalendar class.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew</title>
		<link>http://quickduck.com/blog/2008/11/03/reader-challenge/comment-page-1/#comment-1641</link>
		<dc:creator>Andrew</dc:creator>
		<pubDate>Tue, 04 Nov 2008 12:26:17 +0000</pubDate>
		<guid isPermaLink="false">http://quickduck.com/blog/2008/11/03/reader-challenge/#comment-1641</guid>
		<description>Yeah!  Slow and simple wins the race.  No wait, slow and special.... no wait.....</description>
		<content:encoded><![CDATA[<p>Yeah!  Slow and simple wins the race.  No wait, slow and special&#8230;. no wait&#8230;..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: gerrod</title>
		<link>http://quickduck.com/blog/2008/11/03/reader-challenge/comment-page-1/#comment-1640</link>
		<dc:creator>gerrod</dc:creator>
		<pubDate>Tue, 04 Nov 2008 09:48:13 +0000</pubDate>
		<guid isPermaLink="false">http://quickduck.com/blog/2008/11/03/reader-challenge/#comment-1640</guid>
		<description>Thanks for the answers guys.

Ben&#039;s answer was by far the most comprehensive and I&#039;d lean towards that if I was making a DateTime library (which as Drew suggested, probably already exists).

Andrew&#039;s answer however is much simpler and closer to what I need for this particular purpose.

Here&#039;s what I came up with - essentially a cut down version of Ben&#039;s. Note that &lt;code&gt;itemDate&lt;/code&gt; is the date being tested:

&lt;pre&gt;&lt;code&gt;
DateTime today = DateTime.Today;

DateTime thisWeekStart = today.AddDays(
    DayOfWeek.Sunday - today.DayOfWeek);

DateTime thisWeekEnd = today.AddDays(
    DayOfWeek.Saturday - today.DayOfWeek);

if (itemDate &gt; thisWeekStart &amp;&amp; itemDate.Date &lt;= thisWeekEnd)
    return &quot;This week&quot;;
&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Thanks for the answers guys.</p>
<p>Ben&#8217;s answer was by far the most comprehensive and I&#8217;d lean towards that if I was making a DateTime library (which as Drew suggested, probably already exists).</p>
<p>Andrew&#8217;s answer however is much simpler and closer to what I need for this particular purpose.</p>
<p>Here&#8217;s what I came up with &#8211; essentially a cut down version of Ben&#8217;s. Note that <code>itemDate</code> is the date being tested:</p>
<pre><code>
DateTime today = DateTime.Today;

DateTime thisWeekStart = today.AddDays(
    DayOfWeek.Sunday - today.DayOfWeek);

DateTime thisWeekEnd = today.AddDays(
    DayOfWeek.Saturday - today.DayOfWeek);

if (itemDate &gt; thisWeekStart &amp;&amp; itemDate.Date &lt;= thisWeekEnd)
    return "This week";
</code></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrew</title>
		<link>http://quickduck.com/blog/2008/11/03/reader-challenge/comment-page-1/#comment-1639</link>
		<dc:creator>Andrew</dc:creator>
		<pubDate>Tue, 04 Nov 2008 09:37:26 +0000</pubDate>
		<guid isPermaLink="false">http://quickduck.com/blog/2008/11/03/reader-challenge/#comment-1639</guid>
		<description>&lt;code&gt; 
public static bool InSameWeek(DateTime d1, DateTime d2)
{
   Calendar cal = new GregorianCalendar();
   int weekNo1 = cal.GetWeekOfYear(d1, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
   int weekNo2 = cal.GetWeekOfYear(d2, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);
   return weekNo1 == weekNo2;
}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p><code><br />
public static bool InSameWeek(DateTime d1, DateTime d2)<br />
{<br />
   Calendar cal = new GregorianCalendar();<br />
   int weekNo1 = cal.GetWeekOfYear(d1, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);<br />
   int weekNo2 = cal.GetWeekOfYear(d2, CalendarWeekRule.FirstDay, DayOfWeek.Sunday);<br />
   return weekNo1 == weekNo2;<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Drew Freyling</title>
		<link>http://quickduck.com/blog/2008/11/03/reader-challenge/comment-page-1/#comment-1638</link>
		<dc:creator>Drew Freyling</dc:creator>
		<pubDate>Tue, 04 Nov 2008 01:46:49 +0000</pubDate>
		<guid isPermaLink="false">http://quickduck.com/blog/2008/11/03/reader-challenge/#comment-1638</guid>
		<description>Try the DateJs library. Its a great library with a heap of tools for all your dating needs.</description>
		<content:encoded><![CDATA[<p>Try the DateJs library. Its a great library with a heap of tools for all your dating needs.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben</title>
		<link>http://quickduck.com/blog/2008/11/03/reader-challenge/comment-page-1/#comment-1637</link>
		<dc:creator>Ben</dc:creator>
		<pubDate>Tue, 04 Nov 2008 00:37:58 +0000</pubDate>
		<guid isPermaLink="false">http://quickduck.com/blog/2008/11/03/reader-challenge/#comment-1637</guid>
		<description>I&#039;ll start with a simple implementation that assumes the week begins on a Sunday.

&lt;pre&gt;&lt;code&gt;
/// &lt;summary&gt;
/// Verify two dates are within the same 7 day week where the week begins on a Sunday.
/// &lt;/summary&gt;
public static bool InSameWeekSimple(this DateTime d1, DateTime d2) {   

            if (d2 &lt; d1) { Swap(ref d1, ref d2); } // ensure d1 is less than or equal to d2
            DateTime beginningOfWeekDate2 = d2.AddDays(-(int)d2.DayOfWeek);
            return d1 &gt;= beginningOfWeekDate2 &amp;&amp; d1 &lt;= d2; // verify that d1 is greater than the date that begins d2s week and less than d2.
}
&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code&gt;
 private static void Swap&lt;T&gt;(ref T obj1, ref T obj2) {
            var temp = obj1;
            obj1 = obj2;
            obj2 = temp;
}
&lt;/code&gt;&lt;/pre&gt;

This can be extended to work with a week beginning on a different day to Sunday:

&lt;pre&gt;&lt;code&gt;
public static class ExtensionMethods {

  /// &lt;summary&gt;
  /// Verify two dates are within the same 7 day week where the week begins on a Sunday.
  /// &lt;/summary&gt;
  public static bool InSameWeek(this DateTime d1, DateTime d2) {
    return InSameWeek(d1, d2, DayOfWeek.Sunday);
  }

  /// &lt;summary&gt;
  /// Verify two dates are within the same 7 day week, specifying which   day begins the week.
  /// &lt;/summary&gt;
  public static bool InSameWeek(this DateTime d1, DateTime d2, DayOfWeek weekBeginDay) {
    if (d2 &lt; d1) { Swap(ref d1, ref d2); } // ensure d1 is less than or equal to d2
    DateTime beginningOfWeekDate2 = d2.AddDays(-(int)d2.DayOfWeek); 
    //DateTime beginningOfWeekDate2 = d2.AddDays(((weekBeginDay - d2.DayOfWeek) - 7)%7); 
    return d1 &gt;= beginningOfWeekDate2 &amp;&amp; d1 &lt;= d2; // verify that d1 is greater than the date that begins d2s week and less than d2.
  }

  /// &lt;summary&gt;
  /// Verify two dates are within monday-friday of the same week.
  /// &lt;/summary&gt;
  public static bool InSameWeekdays(this DateTime d1, DateTime d2) {
    if (IsWeekendDay(d1) &#124;&#124; IsWeekendDay(d2)) { return false; }
    if (d2 &lt; d1) { Swap(ref d1, ref d2); } // ensure d1 is less than or equal to d2
    return d1 &gt;= d2.AddDays(-((int)d2.DayOfWeek - (int)DayOfWeek.Monday)) &amp;&amp; d1 &lt;= d2; // verify that d1 is greater than the date that begins d2s week and less than d2.
  }

  /// &lt;summary&gt;
  /// Does the date provided fall on a Saturday or Sunday
  /// &lt;/summary&gt;
  public static bool IsWeekendDay(DateTime date) {
    return date.DayOfWeek == DayOfWeek.Saturday &#124;&#124; date.DayOfWeek == DayOfWeek.Sunday;
  }

  private static void Swap&lt;T&gt;(ref T obj1, ref T obj2) {
    var tempDate = obj1;
    obj1 = obj2;
    obj2 = tempDate;
  }
}

&lt;/code&gt;&lt;/pre&gt;

All of these functions were unit tested and can be verified. 

Do I win?</description>
		<content:encoded><![CDATA[<p>I&#8217;ll start with a simple implementation that assumes the week begins on a Sunday.</p>
<pre><code>
///
<summary>
/// Verify two dates are within the same 7 day week where the week begins on a Sunday.
/// </summary>

public static bool InSameWeekSimple(this DateTime d1, DateTime d2) {   

            if (d2 < d1) { Swap(ref d1, ref d2); } // ensure d1 is less than or equal to d2
            DateTime beginningOfWeekDate2 = d2.AddDays(-(int)d2.DayOfWeek);
            return d1 >= beginningOfWeekDate2 &#038;&#038; d1 < = d2; // verify that d1 is greater than the date that begins d2s week and less than d2.
}
</code></code></pre>
<pre><code>
 private static void Swap<t>(ref T obj1, ref T obj2) {
            var temp = obj1;
            obj1 = obj2;
            obj2 = temp;
}
</t></code></pre>
<p>This can be extended to work with a week beginning on a different day to Sunday:</p>
<pre><code>
public static class ExtensionMethods {

  ///
<summary>
  /// Verify two dates are within the same 7 day week where the week begins on a Sunday.
  /// </summary>

  public static bool InSameWeek(this DateTime d1, DateTime d2) {
    return InSameWeek(d1, d2, DayOfWeek.Sunday);
  }

  ///
<summary>
  /// Verify two dates are within the same 7 day week, specifying which   day begins the week.
  /// </summary>

  public static bool InSameWeek(this DateTime d1, DateTime d2, DayOfWeek weekBeginDay) {
    if (d2 < d1) { Swap(ref d1, ref d2); } // ensure d1 is less than or equal to d2
    DateTime beginningOfWeekDate2 = d2.AddDays(-(int)d2.DayOfWeek);
    //DateTime beginningOfWeekDate2 = d2.AddDays(((weekBeginDay - d2.DayOfWeek) - 7)%7);
    return d1 >= beginningOfWeekDate2 &#038;&#038; d1 < = d2; // verify that d1 is greater than the date that begins d2s week and less than d2.
  }

  ///
<summary>
  /// Verify two dates are within monday-friday of the same week.
  ///
  public static bool InSameWeekdays(this DateTime d1, DateTime d2) {
    if (IsWeekendDay(d1) || IsWeekendDay(d2)) { return false; }
    if (d2 < d1) { Swap(ref d1, ref d2); } // ensure d1 is less than or equal to d2
    return d1 >= d2.AddDays(-((int)d2.DayOfWeek - (int)DayOfWeek.Monday)) &#038;&#038; d1 < = d2; // verify that d1 is greater than the date that begins d2s week and less than d2.
  }

  ///
<summary>
  /// Does the date provided fall on a Saturday or Sunday
  ///
  public static bool IsWeekendDay(DateTime date) {
    return date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday;
  }

  private static void Swap<t>(ref T obj1, ref T obj2) {
    var tempDate = obj1;
    obj1 = obj2;
    obj2 = tempDate;
  }
}

</t></code></pre>
<p>All of these functions were unit tested and can be verified. </p>
<p>Do I win?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
