C # UTC DateTime Request in WCF / .NET
This is a very simple question (hopefully). I am new to working with DateTime to .NET conversion.
I have a WCF service that has a DateTime property - call it BookingDate.
Someone passes this to my WCF service in the format:
The server it sits on is set to UTC timezone (Lisbon, London, Dublin).
When I store the corresponding value in the database, it sets the value:
I assumed that I am mistakenly believing that the .NET framework (as part of the WCF serialization / deserialization process) would output this to .Net Datetime UTC for me (since there is a minus offset of 4 hours as above)
My question is this: I would need to call:
var date = fromClientWCFService.BookingDate.ToUniversalTime();
to get the 21:03 time i expect?
If not, is there a WCF option to pass to my service to convert DateTimes to UTC and not to the server timezone?
Thank you in advance
From 1 answer I can see that DateTimeOffset can be used. Following this, do the following job
var offset = DateTimeOffset.Parse("2012-03-26T17:03:00-0400");
to return the result:
source to share
captures the offset from the specified time (by default it is not UTC, it is determined by the scope of your application, but the most common offset will be from UTC) along with the date / time information and this information will flow through the WCF calls (as well as to the database, assuming it supports the type, in which case SQL Server has a datatype
In fact, using
is the preferred method for handling date and time data in almost all situations . Note on the previous link:
These values are much more common for DateTimeOffset values than those for DateTime values. As a result, consider the DateTimeOffset default date and time type for application development.
source to share