2

I have a service(Language: VB.NET, Framework:.NET 3.5) that returns a dataset from which I read a date. The value of the column in the database is: "1980-03-30 00:00:00.000".

In a client(Language: C#) that uses .NET 3.5, the value is coming thorough as : 3/30/1980 12:00:00 AM

However, when I change the same client to use .NET 4 or .NET 4.5, the date which comes through the service is: 3/29/1980 11:00:00 PM

CodeNewa
  • 127
  • 1
  • 7
  • 4
    What exact code is being used to read the DateTime's? – Arran Nov 26 '13 at 16:33
  • 1
    datetime is dependant on specific locale - ensure that it is unchanged during your tests – Nogard Nov 26 '13 at 16:33
  • I read the date directly from the dataset as follows: Convert.ToDateTime(client.Tables[0].Rows[0]["BIRTHDATE"]); – CodeNewa Nov 26 '13 at 16:36
  • The dataset is the one that is being passed from the service to client. I'm starting to wonder if Serialization causes the issue. – CodeNewa Nov 26 '13 at 16:42

1 Answers1

3

The difference in DateTime values is not because of .Net framework, its because of the locale settings.

Never pass DateTime values through your web service, pass DateTimeOffset and then convert to DateTime type in your client.

Habib
  • 219,104
  • 29
  • 407
  • 436
  • I'm not passing a DateTime object. It's a part of the dataset. – CodeNewa Nov 26 '13 at 16:40
  • @AshishShakya, [returning DataSet is bad](http://stackoverflow.com/questions/1659402/to-return-a-dataset-in-a-web-service-or-not) but its probably off topic here, You have to modify your DataSet and return a column of `DateTimeOffset` type, created from the original `DateTime` object, and use that. – Habib Nov 26 '13 at 16:45
  • The web service is setup to just call a stored procedure and returns the result to the client. In the database, the column is a `DateTime`. I do not have the option of changing how the value is stored in the database at this point. I can read the dataset and transform the datetime to datetimeoffset and pass a processed dataset back to the client. However, is there any thing else that will solve the issue? – CodeNewa Nov 26 '13 at 17:08
  • @AshishShakya, I don't know of any other way, I had this issue once and I end up returning `DateTimeOffset` column instead of `DateTime`, You can get the `DateTimeOffset` in your proc see [this](http://stackoverflow.com/questions/2008522/sql-server-datetime-to-datetimeoffset) or modify the `DataSet` in your web service before sending. – Habib Nov 26 '13 at 17:45
  • Thank you @Habib. I'll try out your suggestions. – CodeNewa Nov 27 '13 at 15:58