【问题标题】:Datetime conversion in Mysql and javascriptMysql 和 javascript 中的日期时间转换
【发布时间】:2015-09-29 22:02:06
【问题描述】:

我正在使用 javascript 和 Mysql 来存储日期时间

     var twoDigits = function (d) {
        if (0 <= d && d < 10) return "0" + d.toString();
        if (-10 < d && d < 0) return "-0" + (-1 * d).toString();
        return d.toString();
    };

    Date.prototype.toMysqlFormat = function () {
        return this.getUTCFullYear() + "-" + twoDigits(1 + this.getUTCMonth()) + "-" + twoDigits(this.getUTCDate()) + " " + twoDigits(this.getUTCHours()) + ":" + twoDigits(this.getUTCMinutes()) + ":" + twoDigits(this.getUTCSeconds());
    };

所以当我发送“2015-07-11 10:00:00”时,它会在 DB 中存储“2015-07-11 04:30:00”

var p = new Date("2015-07-11 10:00:00").toMysqlFormat ();

当我从数据库中检索此值时,我得到“2015-07-10T23:00:00.000Z”。

Using var x = new Date("2015-07-10T23:00:00.000Z"), it gives me 

Sat Jul 11 2015 04:30:00 GMT+0530 (India Standard Time)

这里的 GMT 出错了。这是我收到的UTC时间。我的时区是 +0530

【问题讨论】:

    标签: javascript mysql datetime


    【解决方案1】:

    尝试在“my.cnf”文件中添加您需要的区域

    default_time_zone='+05:30' or '+00:00'
    

    timezone='UTC' or 'GMT'
    

    http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html

    【讨论】:

      猜你喜欢
      • 2017-06-07
      • 1970-01-01
      • 1970-01-01
      • 2011-03-05
      • 1970-01-01
      • 2017-12-10
      • 1970-01-01
      • 2011-07-05
      相关资源
      最近更新 更多