I have a web application in which my server is hosted in US and my client is from India. I have a datetime picker ,which I validate on the server whether the client and server datetime is match or not, but the problem is it will cause invalid due to different timezones of client and server. How do I solve the date time issue
Asked
Active
Viewed 2,558 times
0
-
2http://stackoverflow.com/questions/15169679/how-to-convert-a-datetime-to-specific-timezone-in-c – Jannik Feb 01 '16 at 06:22
-
1`var d = new Date(); var n = d.getTimezoneOffset();` It will gives you UTC offset (in minutes (_local_)), send this info with your dates from client to server. You said server is hosted in US (for example: UTC+10 (_server_)). Difference will be _server_ - _local_. – Slava Utesinov Feb 01 '16 at 06:38
2 Answers
2
Convert time to UTC in your browser and deal with UTC everywhere (except displaying to user) and send date to server in full ISO8601 format so when parsed by server it will be able to convert to its local timezone:
// JavaScript: myDateTimeValue should be of type Date
var utcDateTimeAsString = myDateTimeValue.toISOString();
Than either set this to hidden field that will be send to server (if using regular postback) or just send as part of your AJAX request.
Server side - regular parsing of such string will produce valid local date that corresponds to the same absolute time:
// C# parsing of result of JavaScript call: (new Date()).toISOString();
DateTime localTime = DateTime.Parse("2016-02-01T06:38:05.609Z");
Links if you decide to deal with timezones manually:
- Converting local to UTC in JavaScript - How do you convert a JavaScript date to UTC?
- Obtaining timezone offset if you need to render value server-side - How to get UTC offset in javascript (analog of TimeZoneInfo.GetUtcOffset in C#)
- Converting UTC to local in JavaScript - Convert UTC date time to local date time using JavaScript

Community
- 1
- 1

Alexei Levenkov
- 98,904
- 14
- 127
- 179