<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Chris Danielson&#039;s Blog &#187; Date Range</title>
	<atom:link href="http://www.chrisdanielson.com/tag/date-range/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.chrisdanielson.com</link>
	<description>Trials and Tribulations of a Software Developer</description>
	<lastBuildDate>Wed, 14 Sep 2011 00:00:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>.Net C Sharp Date Range Check Method</title>
		<link>http://www.chrisdanielson.com/2009/07/21/net-c-sharp-date-range-check-method/</link>
		<comments>http://www.chrisdanielson.com/2009/07/21/net-c-sharp-date-range-check-method/#comments</comments>
		<pubDate>Tue, 21 Jul 2009 16:04:50 +0000</pubDate>
		<dc:creator>Chris Danielson</dc:creator>
				<category><![CDATA[.Net Development]]></category>
		<category><![CDATA[C Sharp]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Date Range]]></category>
		<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://www.chrisdanielson.com/?p=58</guid>
		<description><![CDATA[This function is a trivial example of how to do a basic date range check in .Net. I do a few silly conversions from string to timestamp only to serve as an example. View Code CSHARP/* Assumes the local culture. Does a check to see if the current time is within the range of 7 [...]]]></description>
			<content:encoded><![CDATA[<p>This function is a trivial example of how to do a basic date range check in .Net.  I do a few silly conversions from string to timestamp only to serve as an example.</p>

<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p58code3'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p583"><td class="code" id="p58code3"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">/*
Assumes the local culture.  
Does a check to see if the current time is within the range 
  of 7 AM and 1 PM.  If so, returns true.
*/</span>
<span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>IsCurrentDateInRange<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;7:00&quot;</span>, <span style="color: #666666;">&quot;13:00&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
   <span style="color: #008080; font-style: italic;">//The current date is in range.</span>
<span style="color: #008000;">&#125;</span> <span style="color: #0600FF; font-weight: bold;">else</span> <span style="color: #008000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">//The current date is out of range.</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>


<div class="wp_codebox_msgheader"><span class="right"></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p58code4'); return false;">View Code</a> CSHARP</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p584"><td class="code" id="p58code4"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">/*
startTime should be in the format HH:mm such as 09:45
endTime should be in the format HH:mm such as 18:45
The culture is assumed to be the local one which can be 
troubling in applications that extend themselves internationally.
*/</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> IsCurrentDateInRange<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> startTime, <span style="color: #6666cc; font-weight: bold;">string</span> endTime<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
  DateTime dtNow <span style="color: #008000;">=</span> DateTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Now</span><span style="color: #008000;">;</span>
  DateTime dtStart, dtEnd<span style="color: #008000;">;</span>
  <span style="color: #6666cc; font-weight: bold;">int</span> hour <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>, minute <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
  <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> strDt <span style="color: #008000;">=</span> startTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Split</span><span style="color: #008000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">':'</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>strDt<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
     Int32<span style="color: #008000;">.</span><span style="color: #0000FF;">TryParse</span><span style="color: #008000;">&#40;</span>strDt<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span>, <span style="color: #0600FF; font-weight: bold;">out</span> hour<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     Int32<span style="color: #008000;">.</span><span style="color: #0000FF;">TryParse</span><span style="color: #008000;">&#40;</span>strDt<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span>, <span style="color: #0600FF; font-weight: bold;">out</span> minute<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
     dtStart <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DateTime<span style="color: #008000;">&#40;</span>dtNow<span style="color: #008000;">.</span><span style="color: #0000FF;">Year</span>, dtNow<span style="color: #008000;">.</span><span style="color: #0000FF;">Month</span>, dtNow<span style="color: #008000;">.</span><span style="color: #0000FF;">Day</span>, hour, minute, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">else</span>
  <span style="color: #008000;">&#123;</span>
     <span style="color: #008080; font-style: italic;">//TODO: log an error in formatting.</span>
     <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>               
&nbsp;
  strDt <span style="color: #008000;">=</span> endTime<span style="color: #008000;">.</span><span style="color: #0000FF;">Split</span><span style="color: #008000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> <span style="color: #6666cc; font-weight: bold;">char</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span> <span style="color: #666666;">':'</span> <span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>strDt<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
     hour <span style="color: #008000;">=</span> minute <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
     Int32<span style="color: #008000;">.</span><span style="color: #0000FF;">TryParse</span><span style="color: #008000;">&#40;</span>strDt<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #008000;">&#93;</span>, <span style="color: #0600FF; font-weight: bold;">out</span> hour<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
     Int32<span style="color: #008000;">.</span><span style="color: #0000FF;">TryParse</span><span style="color: #008000;">&#40;</span>strDt<span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span>, <span style="color: #0600FF; font-weight: bold;">out</span> minute<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
     dtEnd <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> DateTime<span style="color: #008000;">&#40;</span>dtNow<span style="color: #008000;">.</span><span style="color: #0000FF;">Year</span>, dtNow<span style="color: #008000;">.</span><span style="color: #0000FF;">Month</span>, dtNow<span style="color: #008000;">.</span><span style="color: #0000FF;">Day</span>, hour, minute, <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">else</span>
  <span style="color: #008000;">&#123;</span>
     <span style="color: #008080; font-style: italic;">//TODO: log an error in formatting.</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  TimeSpan tsEndDiff <span style="color: #008000;">=</span> dtEnd <span style="color: #008000;">-</span> dtNow<span style="color: #008000;">;</span>
  TimeSpan tsStartDiff <span style="color: #008000;">=</span> dtNow <span style="color: #008000;">-</span> dtStart<span style="color: #008000;">;</span>
  <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>tsStartDiff<span style="color: #008000;">.</span><span style="color: #0000FF;">TotalSeconds</span> <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">0</span> <span style="color: #008000;">&amp;&amp;</span> tsEndDiff<span style="color: #008000;">.</span><span style="color: #0000FF;">TotalSeconds</span> <span style="color: #008000;">&gt;=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
     <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
  <span style="color: #008000;">&#125;</span>
  <span style="color: #0600FF; font-weight: bold;">else</span> 
     <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://www.chrisdanielson.com/2009/07/21/net-c-sharp-date-range-check-method/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

