moment.js conversions having no effect

I have an input field where the user is required to select a date/time The local machine will always be in either GMT or BST depending on the time of year.

For those who aren't aware of UK time shenanigans:

GMT (Greenwich Mean Time) is always equal to UTC

BST (British Summer Time) is GMT+1 during the summer months

The dates the users enter will be stored in a database that is set to GMT, thus never offsets the time. Hence I need to convert from what the user enters into GMT / UTC.

However my conversions are resulting in the same date being returned.

What am I doing wrong?

https://jsfiddle.net/r68owagL/

Here is the code from the jsFiddle

function log(obj) {
    var html = "<table>"
    for(var member in obj)
    {
        html += "<tr>"
              + "<td>" + member + ": </td>"
              + "<td>" + obj[member].format("YYYY-MM-DD HH:mm:ss") + "</td>"
              + "</tr>";
    }
    html += "</table>";
    document.body.innerHTML = html;
}

var strDate = '2016-07-14 10:51:00';
var obj = {
  n: moment(strDate),                    //Gives: 2016-07-14 10:51:00
  u: moment.utc(strDate),                //Gives: 2016-07-14 10:51:00
  b: moment.tz(strDate, "Europe/London") //Gives: 2016-07-14 10:51:00
}

log(obj);

If you remove the format string and just output the dates like so

 obj[member].format() 

then it is output like so

n:  2016-07-14T10:51:00+02:00
u:  2016-07-14T10:51:00+00:00
b:  2016-07-14T10:51:00+01:00  

So the custom format string just outputs the dates without taking the offsets into account. To take them into account, convert it to UTC before printing them out like so

obj[member].utc().format("YYYY-MM-DD HH:mm:ss")

which results in

n:  2016-07-14 08:51:00
u:  2016-07-14 10:51:00
b:  2016-07-14 09:51:00
链接地址: http://www.djcxy.com/p/29332.html

上一篇: 在mysql数据库中存储时区信息

下一篇: moment.js转换没有效果