`

通过Jquery和yclass获取服务器时间

阅读更多

Jquery:

var date = new Date($.ajax({async: false}).getResponseHeader("Date"));
var time = new Date($.ajax({async: false}).getResponseHeader("Date"));

这样获取的是  GMT+0800 (中国标准时间)。

而这样获得时间 $.ajax({async: false}).getResponseHeader("Date")  是GMT 时间比中国标准时间提前了8个小时。

 

new Date("2014-04-02")             Wed Apr 02 2014 08:00:00 GMT+0800 (中国标准时间) 

new Date("2014-04-02 00:00:00")        Wed Apr 02 2014 00:00:00 GMT+0800 (中国标准时间)

 

new Date("2014-4-2")       Wed Apr 02 2014 00:00:00 GMT+0800 (中国标准时间)

new Date("2014-4-2 00:00:00")    Wed Apr 02 2014 00:00:00 GMT+0800 (中国标准时间)

可以看到 月 和 日 前 加 0 和不加 0 的区别,加 0 的new Date() 时间推后了 8 小时。加上具体的时分秒后就可以了。

 

new Date(Date.parse("2014-04-02"))       Wed Apr 02 2014 08:00:00 GMT+0800 (中国标准时间)

new Date(Date.parse("2014-04-02 00:00:00"))      Wed Apr 02 2014 00:00:00 GMT+0800 (中国标准时间)

 

new Date(Date.parse("2014-4-2"))         Wed Apr 02 2014 00:00:00 GMT+0800 (中国标准时间)

new Date(Date.parse("2014-4-2 00:00:00"))        Wed Apr 02 2014 00:00:00 GMT+0800 (中国标准时间)

parse() 和 直接new Date() 一样,可以看到 月 和 日 前 加 0 和不加 0 的区别,加 0 的parse()时间推后了 8 小时。加上具体的时分秒后就可以了。

注意:Firefox 中parse() 方法中的月和日 必须加上 0 ,否则结果为NaN。

 

new Date("2014,04,01")                Tue Apr 01 2014 00:00:00 GMT+0800 (中国标准时间)
new Date("2014","04","01")            Thu May 01 2014 00:00:00 GMT+0800 (中国标准时间)

以上两种所有浏览器都支持的日期格式

new Date("2014-04-02 00:00:00")   firefox 不支持

new Date("2014-04-02")    Firefox 支持

 

new Date("2014,04,01,00,00,00")     Invalid Date

new Date("2014","04","01","00","00","00")    Thu May 01 2014 00:00:00 GMT+0800 (中国标准时间)

 Date.parse("2014,04,01") 和 Date.parse("2014,04,01,00,00,00")  ie 不支持

 Date.parse("2014","04","01")  生成的日期是 2014年1月1日,通过 Date.parse(new Date("2014","04","01"))生成的日期是正确的。 

 

 

 

yclass:

Y.ajax({
  url : "/user/null.go",
  end : function(data, i) {
   var servernow = Y.getDate(data.date);
   var date = new Date(servernow);

 

    Class.C('current_date', date);    //相当于把时间设置到 current_date  变量里
     ......
  }
 });

 

后面这样可以取到,这样就解决了延迟问题:

var date = new Date(Class.C('current_date')); 

 

 

这样这个Ajax的执行一般有延迟在其它的执行之后,可以把它放在setTimeout函数中,如下:

setTimeout(function(){
     Y.ajax({
             url : "/user/null.go",
             end : function(data, i) {
             var servernow = Y.getDate(data.date);
             var date = new Date(servernow);
  }
 });

 },1000);

 

这样其它地方用到这个时间时就可以正确的初始化使用了。

 

另一篇博客介绍如何通过原生js使用Ajax获取服务器的时间:

http://lfl2011.iteye.com/blog/1701225

 

 

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics