2015-07-11 126 views
1

我使用JavaScript和MySQL存儲日期時間日期時間轉換在MySQL和JavaScript

 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年7月11日10:00:00」,它存儲「 2015年7月11日04:30:00" 在DB

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

當我檢索此值從DB我得到 「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

回答