Solve the Date type is UTC format when json serialization in nodejs

In nodejs, when json is serialized as Date type, it is converted to UTC format by default.

As shown below

zhupengfei@DESKTOP-HJASOE3 MINGW64 /d/MyProject/exp2
$ node
> new Date();
2018-04-24T12:32:55.590Z
>

 

 

The above is just an example. I will use a more specific example to show this situation. We often use Express components in the development of WEB projects.

We created a demo project with express, and now we need to provide an api interface to return some information

Return operator information, this is a normal interface, right?

Let's take a look, the time displayed in the interface log is 20:00 pm, but the JSON data returned by the interface is 12:00, a difference of 8 hours.

why?

The reason is that when JSON is serialized, the date type is converted to the time in UTC by default, and the UTC format has no time zone, or 0 time zone. The client automatically reconverts according to the local time zone.

JSON may be trying to avoid people making mistakes in time zones and causing time inconsistencies. But the problem is that the time must be processed, and it cannot be used without processing.

Can the output include the time zone by default, or can it be used directly without further processing?

The answer is yes. The following describes the processing method in express

In the www file in the bin directory, add the following code

 

copy code
function dateFormat(date, fmt) {
    if (null == date || undefined == date) return '';
    var o = {
        "M+": date.getMonth() + 1, //month
        "d+": date.getDate(), //日
        "h+": date.getHours(), //小时
        "m+": date.getMinutes(), //分
        "s+": date.getSeconds(), //秒
        "S": date.getMilliseconds() //milliseconds
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

Date.prototype.toJSON = function () { return dateFormat(this,'yyyy-MM-dd hh:mm:ss')}
copy code

 

As shown below

 

 

In this way, the output contains the time zone and can be used directly without processing.

 

Summarize:

Again, this is not a problem. At the beginning, we thought it was caused by Sequelize, and we did see a lot of people in github asking why this happened? Can we solve it? There is a lot of scolding, hehe, we are misled. The English is not good, so I can't understand it. I thought it was done by Sequelize, and even got to modify the source code of Sequelize. In fact, there seemed to be some big cows who said that this was not a problem with Sequelize, and they didn't pay attention at that time. Then the next day, I checked it again, and it seems that it is not a problem of Sequelize, but a problem of nodejs? I checked it online, and some people said it was a Nodejs problem. So I consulted colleagues from the goose factory, located it, and finally found the root cause.

It turned out to be the ghost of json serialization.

Therefore, if you encounter a problem, it is correct to check Baidu, but you can't trust it completely. You must think about it yourself and verify it if necessary.

The administrator failed to pass the review, saying that there were too many pictures. There was no way but to delete some pictures and repost them.

As shown below

zhupengfei@DESKTOP-HJASOE3 MINGW64 /d/MyProject/exp2
$ node
> new Date();
2018-04-24T12:32:55.590Z
>

 

 

The above is just an example. I will use a more specific example to show this situation. We often use Express components in the development of WEB projects.

We created a demo project with express, and now we need to provide an api interface to return some information

Return operator information, this is a normal interface, right?

Let's take a look, the time displayed in the interface log is 20:00 pm, but the JSON data returned by the interface is 12:00, a difference of 8 hours.

why?

The reason is that when JSON is serialized, the date type is converted to the time in UTC by default, and the UTC format has no time zone, or 0 time zone. The client automatically reconverts according to the local time zone.

JSON may be trying to avoid people making mistakes in time zones and causing time inconsistencies. But the problem is that the time must be processed, and it cannot be used without processing.

Can the output include the time zone by default, or can it be used directly without further processing?

The answer is yes. The following describes the processing method in express

In the www file in the bin directory, add the following code

 

copy code
function dateFormat(date, fmt) {
    if (null == date || undefined == date) return '';
    var o = {
        "M+": date.getMonth() + 1, //month
        "d+": date.getDate(), //日
        "h+": date.getHours(), //小时
        "m+": date.getMinutes(), //分
        "s+": date.getSeconds(), //秒
        "S": date.getMilliseconds() //milliseconds
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

Date.prototype.toJSON = function () { return dateFormat(this,'yyyy-MM-dd hh:mm:ss')}
copy code

 

As shown below

 

 

In this way, the output contains the time zone and can be used directly without processing.

 

Summarize:

Again, this is not a problem. At the beginning, we thought it was caused by Sequelize, and we did see a lot of people in github asking why this happened? Can we solve it? There is a lot of scolding, hehe, we are misled. The English is not good, so I can't understand it. I thought it was done by Sequelize, and even got to modify the source code of Sequelize. In fact, there seemed to be some big cows who said that this was not a problem with Sequelize, and they didn't pay attention at that time. Then the next day, I checked it again, and it seems that it is not a problem of Sequelize, but a problem of nodejs? I checked it online, and some people said it was a Nodejs problem. So I consulted colleagues from the goose factory, located it, and finally found the root cause.

It turned out to be the ghost of json serialization.

Therefore, if you encounter a problem, it is correct to check Baidu, but you can't trust it completely. You must think about it yourself and verify it if necessary.

The administrator failed to pass the review, saying that there were too many pictures. There was no way but to delete some pictures and repost them.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324887302&siteId=291194637