Date UTC timezone在js json web server中关系探究

来源:互联网 发布:盲打高手打飞字 源码 编辑:程序博客网 时间:2024/06/06 18:46

首先有一个原则要声明:

时区如何变化,Date date = new Date()是一个时间点,不变,可以理解为相对于1970 年 1 月 1 日开始计算到 Date 对象中的时间之间的毫秒数。时区只是显示的格式。


前段使用angularStrap datepicker


首先发现一个问题:日期组件选择时间后,页面直接调用{{datetime}}显示选择时间与当前选择时间差8个小时,此时暴露出时区问题。

思路:

尝试通过angularStrap http://mgcrea.github.io/angular-strap/#/datepickers 设置组件timezone解决问题,timezone默认为空表示当前时区,“UTC”表示世界统一时间也就是上述页面中显示的时间。该方法未能解决问题,因为本身该属性已为空。

发现:

console.log()方式输出选择时间,正常!上述{{}}时间显示应该是组件本身的一个问题,会转换为UTC显示。

server端获得的时间为UTC时间,如果在server端直接操作时间+8hour,问题能够解决,但是!有新的问题暴露出来。

问题:后端获取日期传到前段table显示(包含其他属性),如果此时需要对某一行进行修改,那么此时会发生1 如果修改日期,那么上述方法正常,直接将时间增加8hour。2 如果对其他属性进行修改后保存,对日期没有操作,那么该日期还会在server端进行+时操作。

查找问题~~~

FROM:

http://stackoverflow.com/questions/19564017/how-to-send-angularstrap-datepicker-value-without-timezone

The issue isn't AngularStrap. Its just how javascript dates work and how JSON formats them for transmission. When you turn a javascript date object into a JSON string, it formats the string as UTC.

For example, I'm in Utah and it is now 07:41 on 2013-10-24. If I create a new javascript date and print it to the console it will say:

Thu Oct 24 2013 07:41:19 GMT-0600 (MDT)

If I stringify that same date (using JSON.stringify(date), I get:

"2013-10-24T13:41:47.656Z"

which you can see is not in my current timezone, but is in UTC. So the conversion is happening just before the form gets sent to the server when it gets converted from a javascript object to a JSON string.

前段与server端的接口参数,是以JSON的形式传递,导致date格式传参异常。

但是联想到,之前的一个问题,如果,server端不进行+8操作,暂时抛弃前段日期选择的问题,针对上述2的情况,如果此时对其他属性进行修改,那么该日期不发生变化,也就是说,不会被JSON破坏,为什么???
通过debug发现,后端展示在前段的日期是以date.getTime()形式存在,也就是Long date,这样的格式在date转JSON时不会发生异常

最终解决办法:
将修改,增加js向server端传输通通以getTime()形式传输(这里要注意一下,由于后端展示的代码如果在2情况下,本身就是long date 类型无法调用getTime()需要,先new Date(),再进行操作。)

ok~问题解决。

待续~

前段测试 1

js:var date = new Date()$scope.date = date;html:{{date}}

前端页面显示date为UTC格式

若要解决该问题,可采用如下几种解决办法:

var hereTimeOffset = date.getTimeOffset();$scope.date = date + hereTimeOffset*60000;
第二种解决办法为:

在页面对date添加过滤

<p>date:{{date | date:'yyyy-MM-ddTHH:mm:ss' : 'UTC+8000'}}</p><p>date2:{{date2 | date:'yyyy-MM-ddTHH:mm:ss'}}</p> 
省略timezone 默认浏览器时区

原因分析:


     在js中使用console.log(date)则为正常时间,而在页面中显示时间为UTC时间,则表示js 与 HTML 端日期格式传输中发生了改变

2 直接在前段页面中写JS片段,显示正常

<script>    var date3 = new Date();    alert(date3);    document.getElementById("date3").innerHTML= date3;</script><div id="date3"></div>

前端实验 2 

js:$scope.date = JSON.stringify(date.getTime());html:<p>date:{{date  | date:'yyyy-MM-ddTHH:mm:ss' : ''}}</p>
以上日期显示为浏览器默认时区,timezone若为‘UTC’则显示UTC时区时间。

js:$scope.date = date.getTime();html:<p>date:{{date  | date:'yyyy-MM-ddTHH:mm:ss' : ''}}</p>
不采用JSON.stringify(),结果同上,显示正常。

JAVA API

public long getTime()

Returns the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by thisDate object.

Returns:

the number of milliseconds since January 1, 1970, 00:00:00 GMT represented by this date.

返回的是since January 1, 1970, 00:00:00 GMT

可以认为GMT与UTC相同

探究:

结合上面两个实验和以及server端的传输(server端采用JSON接收参数)

得出:若直接在前段显示,而不是用过滤器,则默认UTC时间。

server端接收JSON stringify转换Date时,会将date转为UTC时间造成时间显示的变化。故采用long类型毫秒传参(无时区概念)。


OK,比较乱,结束~





1 0