【问题标题】:Date is one day off when referenced inside nodejs application在 nodejs 应用程序中引用时,日期为休息一天
【发布时间】:2017-05-17 11:13:41
【问题描述】:

好的,这个问题已被问到here,但是给出的解决方案似乎无法解决我的问题。

我有以下:

当我点击保存时,我会参考这些字段并创建一个日期,如下所示:

this.profile.date_of_birth = new Date(this.editProfileForm.value['year'], this.editProfileForm.value['month'], this.editProfileForm.value['day']);

登录到控制台时显示:

1988 年 12 月 23 日星期五 00:00:00 GMT+1100 (AEDT)

然后我调用在http://localhost:3005 上运行的nodejs 应用程序,该应用程序获取数据并将其保存到mongodb,在保存之前我记录了 date_of_birth 的值,如下所示:

api.put('/update', authenticate, (req, res) => {

    console.log(req.body.date_of_birth);

    // Save properties

});

但是这会记录:

1988-12-22T13:00:00.000Z

今天是休息日……

当用户按下保存时,在创建新日期之前我没有进行任何格式化,所以我不确定为什么当它离开前端应用程序并进入后端应用程序时,日期显示不正确...... .

nodejs 应用程序的实际调用在这里:

saveProfile(profileId: string, token: string, profile: Profile): Observable<any> {

        var body = {
            "id": profile.id,
            "date_of_birth": profile.date_of_birth,
        }


        return this.http.put(UPDATE_EDIT_PROFILE, body, {
            headers: this.setHeaders(token)
        }).map((res: any) => res.json());

    }

谁能推荐可能出错的地方?

【问题讨论】:

    标签: node.js mongodb angular date typescript


    【解决方案1】:

    Fri Dec 23 1988 00:00:00 GMT+1100 (AEDT) 是当前时区的日期。根据1988-12-22T13:00:00.000Z,您的服务器似乎使用UTC

    您可以使用 Date.UTC 创建一个日期对象,其中 standart timezone of UTC 与您的服务器保持一致。

    var year = 1988;
    var month = 12;
    var day = 23;
    console.log(new Date(year, month - 1, day));
    console.log(new Date(Date.UTC(year, month - 1, day)));

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-12
      • 1970-01-01
      • 1970-01-01
      • 2017-08-13
      • 2019-05-11
      相关资源
      最近更新 更多