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:

2012-03-26 22:03


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)

I expected: 2012-03-26 21:03

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:2012-03-26 21:03


source to share

2 answers

Instead of using a structure , you should use . DateTime


The framework DateTimeOffset

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 DateTimeOffset

since 2008).

In fact, using DateTimeOffset

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.



If your server timezone is in Lisbon / London / Dublin, it is not UTC. You will either want to change your server's timezone or express the date / time value in UTC form when it is updated in your database.



All Articles