<p>Some 4 years back, I followed this <a href="http://msdn.microsoft.com/en-us/library/ms973825.aspx" rel="noreferrer">MSDN article</a> for DateTime usage best practices for building a .Net client on .Net 1.1 and ASMX web services (with SQL 2000 server as the backend). I still remember the serialization issues I had with DateTime and the testing effort it took for servers in different time zones.</p>

<p>My questions is this: Is there a similar best practices document for some of the new technologies like WCF and SQL server 2008, especially with the addition of new datetime types for storing time zone aware info.</p>

<p>This is the environment:</p>

<ol>
<li>SQL server 2008 on Pacific Time.</li>
<li>Web Services layer on a different time zone.</li>
<li>Clients could be using .Net 2.0 or .Net 3.5 on different time zones. If it makes it easy, we can force everyone to upgrade to .Net 3.5. :)</li>
</ol>

<p>Any good suggestions/best practices for the data types to be used in each layer?</p>

[{'answer_id': 65314, 'author': 'Abe Heidebrecht', 'author_id': 9268, 'author_profile': 'https://Stackoverflow.com/users/9268', 'pm_score': 5, 'selected': True, 'text': '<p>I think the best way of doing this is to always pass the object as UTC, and convert to local time on the clients.  By doing so, there is a common reference point for all clients.</p>\n\n<p>To convert to UTC, call ToUniversalTime on the DateTime object.  Then, on the clients, call ToLocalTime to get it in their current time zone.</p>\n'}, {'answer_id': 65376, 'author': 'Mark', 'author_id': 9303, 'author_profile': 'https://Stackoverflow.com/users/9303', 'pm_score': 1, 'selected': False, 'text': "<p>I had good luck with just keeping the DateTime data type and always storing it as GMT.  In each layer, I'd adjust the GMT value to the local value for the layer.</p>\n"}, {'answer_id': 65474, 'author': 'Jesse C. Slicer', 'author_id': 3312, 'author_profile': 'https://Stackoverflow.com/users/3312', 'pm_score': 2, 'selected': False, 'text': '<p>As long as your web services layer and client layer use the .NET DateTime type, it should serialize and deserialize properly as an SOAP-standard local date/time w/ Timezone information such as:</p>\n\n<p>2008-09-15T13:14:36.9502109-05:00</p>\n\n<p>If you absolutely, positively must know the timezone itself (i.e. the above could be Eastern Standard Time or Central Daylight Time), you need to create your own datatype which exposes those pieces as such:</p>\n\n<pre><code>[Serializable]\npublic sealed class MyDateTime\n{\n    public MyDateTime()\n    {\n        this.Now = DateTime.Now;\n        this.IsDaylightSavingTime = this.Now.IsDaylightSavingTime();\n        this.TimeZone = this.IsDaylightSavingTime\n            ? System.TimeZone.CurrentTimeZone.DaylightName\n            : System.TimeZone.CurrentTimeZone.StandardName;\n    }\n\n    public DateTime Now\n    {\n        get;\n\n        set;\n    }\n\n    public string TimeZone\n    {\n        get;\n\n        set;\n    }\n\n    public bool IsDaylightSavingTime\n    {\n        get;\n\n        set;\n    }\n}\n</code></pre>\n\n<p>then your response would look like:</p>\n\n<pre><code>&lt;Now&gt;2008-09-15T13:34:08.0039447-05:00&lt;/Now&gt;\n&lt;TimeZone&gt;Central Daylight Time&lt;/TimeZone&gt;\n&lt;IsDaylightSavingTime&gt;true&lt;/IsDaylightSavingTime&gt;\n</code></pre>\n'}, {'answer_id': 70202, 'author': 'Ray Lu', 'author_id': 11413, 'author_profile': 'https://Stackoverflow.com/users/11413', 'pm_score': 2, 'selected': False, 'text': '<p>UTC/GMT would be consistent in distributed environment. </p>\n\n<p>One important thing is that specify the datetimeKind after populating your DateTime property with the value from database.</p>\n\n<pre><code>dateTimeValueUtcKind = DateTime.SpecifyKind(dateTimeValue, DateTimeKind.Utc);\n</code></pre>\n\n<p><a href="http://msdn.microsoft.com/en-us/library/system.datetime.specifykind.aspx" rel="nofollow noreferrer">See MSDN</a></p>\n'}, {'answer_id': 76301, 'author': 'Joe', 'author_id': 13087, 'author_profile': 'https://Stackoverflow.com/users/13087', 'pm_score': 4, 'selected': False, 'text': '<p>One big issue is that WCF serialization doesn\'t support xs:Date.  This is a big problem as if all you want is a date, you shouldn\'t be forced to be concerned about time zones.  The following connect issue discusses some of the problems: <a href="http://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=349215" rel="noreferrer">http://connect.microsoft.com/wcf/feedback/ViewFeedback.aspx?FeedbackID=349215</a></p>\n\n<p>If you want to represent a point in time unambiguously, i.e. not just the date part, you can use the DateTimeOffset class if you have .NET 3.5 on both client and server.  Or for interoperability, always pass date/time values as UTC.</p>\n'}, {'answer_id': 65071586, 'author': 'Laobu', 'author_id': 7426767, 'author_profile': 'https://Stackoverflow.com/users/7426767', 'pm_score': 0, 'selected': False, 'text': '<p>For cases where the datetime object should simply stay the same use JsonConvert:</p>\n<pre><code>DateTime now = DateTime.Now;\nstring json = JsonConvert.SerializeObject(now);\nDateTime nowJson = JsonConvert.DeserializeObject&lt;DateTime&gt;(json);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/65164', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/4337/']