MongoDB日期时间值未正确存储

时间:2014-10-16 08:46:41

标签: javascript mongodb

此处App Store日期和时间值采用ISO 8601格式。但它为时间存储了一些不同的值。我保存了一些时间值3.00 AM,但它存储了像这样的mongodb "start_time": { "$date": "2014-10-15T21:30:00.000Z" }, 那是为什么?

function json_homepage() {   
    var self = this;
    var User = MODEL('user').schema;
    var model = self.body;
    var data = self.post;

    var date = new Date().toLocaleDateString("en-US");
   // var result = new Date(date + " " + time);

    var appoinment = new Appoinment({
        start_time   : new Date(date + " " + data.start_time),
        end_time     : new Date(date + '' + data.end_time),
        service      : model.service
    }).save(function(err) {
        console.log(data.start_time);
        if (err)
            self.throw500(err);
        // Read all users
      User.find(self.callback());
    });

---- ---更新

@ Wizard's Answer -  It's correct. MongoDB uses ISODate to save date value. The last character Z means zero timezone. After adding your timezone, it's the value you want. So, it's only a different display format. – Wizard

1 个答案:

答案 0 :(得分:1)

您可以尝试更改此内容:

var date = new Date().toLocaleDateString("en-US");

对此?

var t = new Date();
var d = t.getDate();
var m = t.getMonth() + 1;
var y = t.getFullYear();

var date = m + '/' + d + '/' + y;

或许日期会被改变到某个地方的另一个时区,虽然我不知道在哪里。你必须要仔细查看。

我相信"2014-10-15T21:30:00.000Z",其中Z = ZULUUTC。你的服务器/ mongo时区是什么?