<?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>DotBert &#187; datetime</title>
	<atom:link href="http://dotbert.loedeman.nl/category/datetime/feed" rel="self" type="application/rss+xml" />
	<link>http://dotbert.loedeman.nl</link>
	<description>.NET thoughts by Bert Loedeman</description>
	<lastBuildDate>Thu, 18 Mar 2010 12:49:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Calculate age (for instance, on reference data)</title>
		<link>http://dotbert.loedeman.nl/calculate-age-for-instance-on-reference-data</link>
		<comments>http://dotbert.loedeman.nl/calculate-age-for-instance-on-reference-data#comments</comments>
		<pubDate>Wed, 02 Sep 2009 09:53:01 +0000</pubDate>
		<dc:creator>Bert Loedeman</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[tip]]></category>

		<guid isPermaLink="false">http://dotbert.loedeman.nl/?p=133</guid>
		<description><![CDATA[Now and then, I have to calculate ages (for instance, on a reference date). The method to calculate ages in .NET is pretty simple, although it can be &#8216;a pain in the ass&#8217; to find out how simple it should have been   &#8230; Enjoy the code   !

public static class DateTimeHelper
{
  [...]]]></description>
			<content:encoded><![CDATA[<p>Now and then, I have to calculate ages (for instance, on a reference date). The method to calculate ages in .NET is pretty simple, although it can be &#8216;a pain in the ass&#8217; to find out how simple it should have been <img src='http://dotbert.loedeman.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' title="Calculate age (for instance, on reference data)" />  &#8230; Enjoy the code <img src='http://dotbert.loedeman.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' title="Calculate age (for instance, on reference data)" />  !</p>
<pre class="brush: csharp;">
public static class DateTimeHelper
{
    public static int CalculateAge(DateTime birthday)
    {
        return CalculateAge(DateTime.Now, birthday);
    }

    public static int CalculateAge(DateTime referenceDate, DateTime birthday)
    {
        // calculate age in years on the given reference date.
        var comparisonDate = new DateTime(birthday.Year, referenceDate.Month, referenceDate.Day);

        return (comparisonDate.Date &lt; birthday.Date)
                      ? referenceDate.Year - birthday.Year - 1
                      : referenceDate.Year - birthday.Year;
    }
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://dotbert.loedeman.nl/calculate-age-for-instance-on-reference-data/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Logging duration with System.Diagnostics.StopWatch instead of using DateTime.Now</title>
		<link>http://dotbert.loedeman.nl/logging-duration-with-systemdiagnosticsstopwatch-instead-of-using-datetimenow</link>
		<comments>http://dotbert.loedeman.nl/logging-duration-with-systemdiagnosticsstopwatch-instead-of-using-datetimenow#comments</comments>
		<pubDate>Fri, 09 Jan 2009 13:14:00 +0000</pubDate>
		<dc:creator>Bert Loedeman</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Stopwatch]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[duration logging]]></category>

		<guid isPermaLink="false">http://www.loedeman.net/wordpress/?p=27</guid>
		<description><![CDATA[Still using two DateTime.Now calls to determine the duration of a certain call execution, like beneath?

DateTime start = DateTime.Now;
DoSomeCall();
double secondsToLog = (DateTime.Now - start).TotalSeconds;

This is not at all necessary: using the StopWatch class. The StopWatch class is more efficient than any other solution provided or created in .NET since it uses low-level API calls and [...]]]></description>
			<content:encoded><![CDATA[<p>Still using two DateTime.Now calls to determine the duration of a certain call execution, like beneath?</p>
<pre class="brush: csharp;">
DateTime start = DateTime.Now;
DoSomeCall();
double secondsToLog = (DateTime.Now - start).TotalSeconds;
</pre>
<p>This is not at all necessary: using the <a href="http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx">StopWatch</a> class. The StopWatch class is more efficient than any other solution provided or created in .NET since it uses low-level API calls and supports a high-resolution performance counter (when there is hardware and software support).</p>
<pre class="brush: csharp;">
Stopwatch watch = new Stopwatch();
watch.Start();
DoSomeCall();
watch.Stop();
double secondsToLog = watch.Elapsed.TotalSeconds;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://dotbert.loedeman.nl/logging-duration-with-systemdiagnosticsstopwatch-instead-of-using-datetimenow/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>IsDateTime in .NET for both ticks and string representation</title>
		<link>http://dotbert.loedeman.nl/isdatetime-in-net-for-both-ticks-and-string-representation</link>
		<comments>http://dotbert.loedeman.nl/isdatetime-in-net-for-both-ticks-and-string-representation#comments</comments>
		<pubDate>Fri, 09 Jun 2006 09:16:00 +0000</pubDate>
		<dc:creator>Bert Loedeman</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[conversion]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[datetime check]]></category>
		<category><![CDATA[datetime ticks]]></category>
		<category><![CDATA[expression]]></category>
		<category><![CDATA[isdatetime]]></category>

		<guid isPermaLink="false">http://www.loedeman.net/wordpress/?p=14</guid>
		<description><![CDATA[Some while ago I developed a Settings system, where the SettingValue type should be checked. The DateTime.TryParse function did not do all the work I wanted it to do, so I thought &#8216;why not develop my own workaround&#8217;   ? It is posted below&#8230;

/// &#60;summary&#62;
/// The IsDateTime function returns whether the given expression is [...]]]></description>
			<content:encoded><![CDATA[<p>Some while ago I developed a Settings system, where the SettingValue type should be checked. The DateTime.TryParse function did not do all the work I wanted it to do, so I thought &#8216;why not develop my own workaround&#8217; <img src='http://dotbert.loedeman.nl/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' title="IsDateTime in .NET for both ticks and string representation" />  ? It is posted below&#8230;</p>
<pre class="brush: csharp;">
/// &lt;summary&gt;
/// The IsDateTime function returns whether the given expression is a DateTime or not.
/// &lt;/summary&gt;
/// &lt;param name=&quot;expression&quot;&gt;Expression to check for being DateTime.&lt;/param&gt;
/// &lt;returns&gt;Boolean&lt;/returns&gt;
/// &lt;remarks&gt;
/// The DateTime expression can be given as ticks or as normal DateTime representation.
/// &lt;/remarks&gt;
public static bool IsDateTime(object expression) {
if(expression == null) return false;Bert Loedeman

string stringExpression = System.Convert.ToString(expression);
if(String.IsNullOrEmpty(stringExpression)) return false;

// DateTime can be specified in ticks. In that case the string should be convertible
// to a long (System.Int64) and fit in a specific range.
long retLong;
if(Int64.TryParse(stringExpression, out retLong)) {
// Ticks: 0 = DateTime.MinValue, 0x2bca2875f4373fff = DateTime.MaxValue
if((retLong &lt; DateTime.MinValue.Ticks) || (retLong &gt; DateTime.MaxValue.Ticks)) return false;
return true;
}

DateTime retDateTime;
return DateTime.TryParse(stringExpression, out retDateTime);
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://dotbert.loedeman.nl/isdatetime-in-net-for-both-ticks-and-string-representation/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
