Click here to Skip to main content
15,849,197 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

When we pass a DateTime data type to a .NET web service or when data is returned in a DataTable back to the client, all the DateTime fields are converted to match the destination timezone. For example, if the server runs in Eastern Time (GMT-5) and the Client runs in Indian Time (GMT+5:30), and we pass 6:00 PM to the server, the time is reflected at the server as 7:30 AM. Whereas this is okay in most situations because both times represent the same point in time, it is highly undesirable when we want to make a comparisons with a field in the database, etc. One solution I have come across on googling this is storing values as UTC in database, but this is not practical when we integrate web service into existing systems. I have my own ways of dealing with this issue (using some time functions, etc,), but i would like to know if there is something built into .NET (preferably .NET 2.0) to get around with this.

Thanks in advance
Posted

You should always store time values as UTC. There is absolutely no reason why you would ever need to store them as anything else. Only in that way can you be sure that the value can be displayed correctly for any user, in any part of the world.
 
Share this answer
 
Comments
Nejimon CR 24-Jan-11 10:35am    
Hi Richard,
It makes sense if we are building a project from scratch. I have a database where more than 100,000 records already and it is growing. The database is accessed by applications locally on the server via network as well as people from outside via web service. It is not practical to write them to database again.

Furthermore, even if you store them in database as UTC, when a particular record is retrieved and sent to the client in a DataTable, the date will get converted by 10:30 hours at the client (which is apparently wrong).
Richard MacCutchan 24-Jan-11 10:54am    
OK, you already have the data so you are stuck.
I don't understand the second paragraph; what do you mean "data will get converted by 10:30 hours at the client"?
Nejimon CR 24-Jan-11 11:11am    
My database is MS Access. The original data contains two date fields. The client applications (desktop applications) request data between two dates (say 01/10/2011 and 01/20/2011). At server, a web method retrieves the qualified records and sends them to the client in a data table. Suppose one of the rows in the data table originally contains a date 01/11/2011 7:30 AM, which is retrieved from database. The original data in database contains no timezone information. When this data reaches the client, ideally it should reflect the same time, right because the database contains that value and no reason to convert to any other timezone. But when this field reaches the client, the client sees the time as 01/11/2011 6:00 PM (because my server-client time difference is 10:30 hours). The end user thinks that the original data was 6:00 PM whereas the original data in the database is only 7:30 AM.
Richard MacCutchan 24-Jan-11 11:48am    
Well that's all fine as long as all your clients are in the same timezone.
Nejimon CR 24-Jan-11 11:21am    
The main thing I need to know is this: Is there any way to send/receive DateTime fields to and from web service without them being converted at the other end (whatever sent is reflected at the other end too, more or less like a string)?
http://stackoverflow.com/a/6935884/ has the best answer I've seen. DataColumn has a DateTimeMode member. If you change it from UnspecifiedLocal (the default?) to Unspecified, .NET quits sending the time zone. This works great for us. YMMV
 
Share this answer
 
Comments
Ajit Kumar Nayak 8-Nov-16 1:08am    
it works for me. thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900