客户端时间显示而不是服务器时间在javascript [英] Client Time shown instead of server Time in javascript

查看:123
本文介绍了客户端时间显示而不是服务器时间在javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在javascript Date()函数的奇怪问题中,这些是细节


服务器端:Struts(ActionForm,Action类,jsp)

客户端:jQuery,javascript


现在,我需要在页面上的服务器时间并使用javascript进行操作。所以,我在action类中做了$ code> Calendar.getInstance()。getTimeinMillis(); ,并将其保存为ActionForm属性。



现在,在客户端,我从styleId获得了长的值(timeinMillis)。但是,要操作,当我做

  var curTime = parseInt($(#serverTime)。val()); 
var serverTime = new Date(curTime);

现在,服务器时间是提供客户端机器日期而不是服务器日期,尽管我们提供服务器的timeinMillis。



这个奇怪的部分是当我传递一个日期的字符串值,而不是从服务器传递长的timeinMillis,并将其作为参数传递时,它的工作正常。



任何想法?

解决方案

最后解决了上述提示的帮助。 p>

从java方面,采用UTC的偏移量与day light saving选项。



所以在Action类中

 日历c = Calendar.getInstance(); 
TimeZone z = c.getTimeZone();
int offset = z.getOffset(c.getTimeInMillis());

这将给出本地时区和UTC时区的偏移量,包括日光节约



在javascript中,我使用了

  var now = new Date(); 
var now_utc = new Date(now.getUTCFullYear(),now.getUTCMonth(),now.getUTCDate(),now.getUTCHours(),now.getUTCMinutes(),now.getUTCSeconds());
var sd = new Date(now_utc.getTime()+ offset);
alert(Server time is+ sd.toLocaleString());

而且,这样可以节省正常的服务器时间。



非常感谢您的帮助,没有他们,将无法实现。干杯!


I am into strange issue of javascript Date() function, these are the details

Server Side : Struts (ActionForm, Action class, jsp)
Client Side : jQuery, javascript

Now, I need a server time on the page and manipulate using javascript. So, I did Calendar.getInstance().getTimeinMillis(); in the action class and save it as an ActionForm attribute.

Now, at the client side, I got the long value (timeinMillis) from the styleId. But, to manipulate, when I do

var curTime = parseInt($("#serverTime").val());
var serverTime = new Date(curTime);

Now, the serverTime is providing client machine date and not server date though we are providing timeinMillis of server.

The strange part is when I pass string value of a date instead of long timeinMillis from server and pass it as an argument, it works well.

Any idea?

解决方案

Finally resolved the issue with help of the above hints.

From java side, taken the offset from the UTC with the day light saving option.

so, in Action class

Calendar c = Calendar.getInstance();
TimeZone z = c.getTimeZone();
int offset = z.getOffset(c.getTimeInMillis());

This gives the offset of local timezone and UTC timezone including day light saving

At the javascript, I used,

var now = new Date(); 
var now_utc = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(),  now.getUTCHours(), now.getUTCMinutes(), now.getUTCSeconds());
var sd = new Date(now_utc.getTime() + offset);
alert("The Server time is " + sd.toLocaleString());

And, this gives correct server time with the day light saving options.

Thanks a lot for your help, without them, would not be possible to work it out. Cheers!

这篇关于客户端时间显示而不是服务器时间在javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆