This is a very simple, (hopefully) question. I am new to working with DateTime conversion in .NET.
I have a WCF service which has a DateTime property – call it BookingDate.
Someone passes that to my WCF service in the format:
<a:BookingDate>2012-03-26T17:03:00-04:00</a:BookingDate>
The server that it is sitting on is set to a timezone of UTC (Lisbon, London, Dublin).
When I store the corresponding value in the database, it sets the value to be:
2012-03-26 22:03
I assumed, I think incorrectly that the .NET framework (as part of the WCF serialize/deserialize process) would pop this into a .Net Datetime of UTC for me (as there is the minus offset of 4 hours as above)
I was expecting: 2012-03-26 21:03
My question is thus: would I need to call:
var date = fromClientWCFService.BookingDate.ToUniversalTime();
in order to get the 21:03 time that I am expecting?
If not, is there a WCF setting to tell my service to convert DateTimes to UTC, rather than the server timezone?
Thanks in advance
Mark
EDIT:
From 1 answer, I can see that DateTimeOffset can be used. Following on from this, would the following work: var offset = DateTimeOffset.Parse("2012-03-26T17:03:00-0400"); to return the result: 2012-03-26 21:03
Instead of using the
DateTimestructure, you should use theDateTimeOffsetstructure.The
DateTimeOffsetstructure captures the offset from a specified time (it’s not UTC by default, it’s defined by the scope of your application, but the most common offset would be from UTC) along with the date/time information, and that information will flow through WCF calls (as well as to a database, assuming it supports the type. SQL Server in this case has thedatetimeoffsetdata type from 2008 on).As a matter of fact, using
DateTimeOffsetis the preferred methods of dealing with date/time data in almost all situations. Note from the previous link: