zoukankan      html  css  js  c++  java
  • nodejs 服务端时间问题

    nodejs构建的服务端,由于时区的问题会导致返回的日期不正确,为了修正日期问题,对Date增加全局的toJSON方法来修正此问题,代码如下:
    注:如果使用的是express框架,则在app.js的首部增加如下代码,其他框架根据实际情况贴入即可
    Date.prototype.toJSON = function () {
      var timezoneOffsetInHours = -(this.getTimezoneOffset() / 60); //UTC minus local time
      var sign = timezoneOffsetInHours >= 0 ? '+' : '-';
      var leadingZero = (Math.abs(timezoneOffsetInHours) < 10) ? '0' : '';
    
      //It's a bit unfortunate that we need to construct a new Date instance
      //(we don't want _this_ Date instance to be modified)
      var correctedDate = new Date(this.getFullYear(), this.getMonth(),
          this.getDate(), this.getHours(), this.getMinutes(), this.getSeconds(),
          this.getMilliseconds());
      correctedDate.setHours(this.getHours() + timezoneOffsetInHours);
      var iso = correctedDate.toISOString();//.replace('Z', '').replace('T',' ').replace(/.[d]+/g,'');
    
      return iso;
    };


    来自:https://blog.csdn.net/joney1/article/details/104229988?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control&dist_request_id=561128c0-8601-4bc7-9dca-62e76969dfd8&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.control
  • 相关阅读:
    springboot初始篇(一)
    SpringBoot使用数据库JdbcTemplate(三)
    java实现分页查询
    设计模式之单例模式
    ❤️考研数学公式❤️
    ❤️图的遍历❤️
    图的存储
    图的基本概念
    森林与二叉树的应用
    树相关的代码题
  • 原文地址:https://www.cnblogs.com/setbug/p/14443670.html
Copyright © 2011-2022 走看看