javascript 在javascript中显示客户端时间而不是服务器时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11328278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Client Time shown instead of server Time in javascript
提问by Felix Christy
I am into strange issue of javascript Date()
function, these are the details
我对javascriptDate()
函数的奇怪问题感兴趣,这些是细节
Server Side : Struts (ActionForm, Action class, jsp)
Client Side : jQuery, javascript
服务器端:Struts(ActionForm、Action 类、jsp)
客户端: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.
现在,我需要页面上的服务器时间并使用 javascript 进行操作。所以,我Calendar.getInstance().getTimeinMillis();
在 action 类中做 了并将其保存为 ActionForm 属性。
Now, at the client side, I got the long value (timeinMillis) from the styleId. But, to manipulate, when I do
现在,在客户端,我从 styleId 中获得了长值 (timeinMillis)。但是,为了操纵,当我做
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.
现在,虽然我们提供服务器的 timeinMillis,但 serverTime 提供的是客户端机器日期而不是服务器日期。
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.
奇怪的是,当我从服务器传递日期的字符串值而不是 long timeinMillis 并将其作为参数传递时,它运行良好。
Any idea?
任何的想法?
回答by Sunil Chavan
This is because your javascript runs on client machine, so when you create new Date() in javascript it picks up the client machine time.
这是因为您的 javascript 在客户端机器上运行,所以当您在 javascript 中创建 new Date() 时,它会获取客户端机器时间。
Answer to second query if you pass the sever date as string it will create date object of that date.
如果您将服务器日期作为字符串传递,则回答第二个查询,它将创建该日期的日期对象。
Use below function
使用以下功能
function calcTime(offset) {
// create Date object for current location
d = new Date();
// convert to msec, add local time zone offsetand get UTC time in msec
utc = d.getTime() + (d.getTimezoneOffset() * 60000);
// create new Date object with supplied offset
nd = new Date(utc + (3600000*offset));
// return time as a string
return "The Server time is " + nd.toLocaleString();
}
Here you need to pass the offset of your server time with UTC
在这里,您需要使用 UTC 传递服务器时间的偏移量
回答by Felix Christy
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.
从 java 端,使用夏令时选项从 UTC 中获取偏移量。
so, in Action class
所以,在 Action 类中
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
这给出了本地时区和 UTC 时区的偏移量,包括夏令时
At the javascript, I used,
在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("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!
非常感谢您的帮助,没有他们,就不可能解决这个问题。干杯!
回答by david a.
If your server and client time are in different timezones and you want to show this difference to the user, then you have to send timezone information to the client as well.
如果您的服务器和客户端时间在不同的时区,并且您想向用户显示这种差异,那么您还必须向客户端发送时区信息。
Calendar.getInstance().getTimeinMillis()
returns the number of milliseconds since the epoch, but it does not say anything about the timezone. It's just a number of milliseconds since midnight 1970/1/1 UTCand at any instant in time, this number is the same on any system (well, given the time is synced), regardless of system's timezone. This might give you the impression that you were getting client time instead of servers.
Calendar.getInstance().getTimeinMillis()
返回自纪元以来的毫秒数,但它没有说明时区。从UTC时间 1970 年 1 月 1 日午夜开始,这只是几毫秒,无论系统时区如何,这个数字在任何系统上都是相同的(好吧,假设时间是同步的)。这可能会给您的印象是您获得的是客户端时间而不是服务器时间。
The reason why this works with string representation of date is the timezone information included in it, and Javascript's ability to parse that and take it into account when constructing the Date
object.
这与日期的字符串表示一起工作的原因是其中包含的时区信息,以及 Javascript 在构造Date
对象时解析和考虑它的能力。
回答by Liko
A simple solution in PHP:
PHP中的一个简单解决方案:
var phpDate = '<?php echo date('Y/m/d H:m:s');?>'
, serverDate = new Date(phpDate)
, month = serverDate.getMonth()+1 //getMonth() returns a 0-based number.
, day = serverDate.getDate()
, year = serverDate.getFullYear();
console.log(year + '-' + month + '-' + day);