Till today I am using DateTime.Now;
function to save the time and data of users. Well, at later stage I used JavaScript to get the clien开发者_StackOverflow社区t's date and time to maintain Time Zone. But when I change my system's date and time and try that JavaScript. It show the changed time of my computer. I need how to get clients correct current date and time value. How to get it.
Please do not exceed answer more than C# & JavaScripts.
When working with clients/servers from different time zones you should use UTC
For example in C# use DateTime.UTCNow and not DateTime.UTC. Along each time you save you should save the timezone as well, later on you'll be able to calculate the local time. It works also for daylight saving changes.
You seem to be asking "How do I determine the client's real timezone even when their computer's clock is set incorrectly?" If so, please update your question to say so more clearly.
You can't do this from JavaScript: how could it know any time or timezone other than what the computer tells it (both of which are based on what the user has set)?
On the server-side you could try an IP-address to timezone mapping but really that's still just an educated guess. (Google it if you want to pursue it anyway.)
You could hack into their web-cam and hope that it is pointed at a clock on the wall.
On client, when client send a request with javascript, get new Date().toJSON() value and send it to server. toJSON() returns current client datetime in GMT+0000 timezone (Greenwich):
var dtStr=new Date().toJSON();
On server, in C#:
DateTime dtServer=DateTime.Now;
DateTime dtClient=DateTime.Parse(Request.QueryString["dtStr"]); // Or Request.Form
double requestTimeDelta=10f; // max time in seconds between client starts the request,
// and this code lines is executed.
double secondsDelta=2f;
DateTime max=dtClient.AddSeconds(requestTimeDelta+secondsDelta);
DateTime min=dtClient.AddSeconds(-secondsDelta);
if (max>=dtServer && min<=dtServer) {
// time is correct
}
else
{
// time is incorrect
}
Above code is required, that time on client and on server has been synchronized with global clocks (internet timesource servers, for example). To avoid this problem you can calculate and save (in Session, for example) initial (on first request) difference between client and server clocks, and add this 'difference' to max
and min
variables. This behaviour is simply to coding, and not showed in code above.
DateTime.Now
runs on the server and gets the datetime as configured on the server and not on the client.
You will have to use javascript to get the clients Time, you can pass the value via a hidden field which could be stored using new Date()
method of javascript. to get the offset from GMT use Date.getTimezoneOffset()
which would give the offset in minutes say like for + 1 GMT it would return -60.
It would be nice to maintain all datetime information in GMT format and then use the offset for client needs.
You can't get the actual machine time. You can attempt to calculate the timezone of the client and then adjust accordingly.
A quick search brought up this post that implements a solution which addresses some of the problems with using the JavaScript Date
object.
Always maintain your datetime in one format on the server side. I prefer DateTimeOffset and that takes care of storing it in the right format for me.. You should definitely read up on that : http://msdn.microsoft.com/en-us/library/system.datetimeoffset.aspx
Regarding rendering it on the client side well you have two options:
A) Store the clients timezone in a cookie as and when he logs in and then use this timezone to serve all your datetime to our client.
B) or you need to perform your time voodoo on the client side for best results and this can be achieved using javascript. you need to make sure you pass one value (say UTC) back to your client and you need to parse your time to UTC too..
here is a neat function to parse your timeto UTC before sending it to the server
function getDateAsUTC(time, date) {
var regExTime = /^(\d{1,2}):(\d{2})([AP]M)?$/i;
var invalidTimeString="invalid time";
if (segmentsTime = time.replace(" ", "").match(regExTime)) {
if (segmentsTime[3]) {
// 12-hour time format with am/pm
if (parseInt(segmentsTime[1],10) < 1 || parseInt(segmentsTime[1],10) > 12) {
time = invalidTimeString;
}
}
else {
// 24-hour time format
if (parseInt(segmentsTime[1],10) > 23) {
time = invalidTimeString;
}
}
if (time !== invalidTimeString && parseInt(segmentsTime[2],10) > 59) {
time = invalidTimeString;
}
}
else {
time = invalidTimeString;
}
if (time !== invalidTimeString) {
var jsDateFriendlyTime = segmentsTime[1] + ":" + segmentsTime[2] + " " + segmentsTime[3];
return new Date(date + " " + jsDateFriendlyTime).toGMTString().replace("UTC", "GMT");
}
else{
return time;
}
}
and for rendering the time in the correct format you could use the toLocaleDateString() method.
精彩评论