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
  • 相关阅读:
    cisco 4500X 交换机限速
    HPE 交换机基础配置
    MySQL数据库之主从复制
    MySQL复制线程状态转变
    MySQL数据库备份之mysqldump
    MySQL数据库之慢查询日志
    MySQL数据库之多线程备份工具mydumper
    MySQL数据库之索引
    MySQL之二进制日志
    MySQL数据库之sql_mode解释
  • 原文地址:https://www.cnblogs.com/setbug/p/14443670.html
Copyright © 2011-2022 走看看