0

I have required 'moment' and 'moment-timezone'

var timeZone = require('moment-timezone');
var moment = require('moment');

And insert to db, the Timezone I need is Asia/Ho_Chi_Minh (GMT+7):

var date = moment(new Date()); 
var d = date.tz('Asia/Ho_Chi_Minh').format();
console.log(d);
var userSchema = mongo.Schema({
    name: String,
    date: {type: Date, "default": d}
});
var users = mongo.model('users', userSchema);

users.create({name: "HYA"}, function(err, res){
    if(err) throw err;
    console.log(res);
})

But value time still default

enter image description here

Thank you!!

  • It's not clear what you are expecting. Is the DB storing the wrong time or do you not like the UTC representation? Mongo stores dates as milliseconds since the Unix epoch, which means it doesn't have a concept of timezones. You will need to convert the date to the timezone you want. – Mark Nov 10 '17 at 16:31
  • I suggest that you always use UTC in the database. Then you can convert it to the local timezone wherever your clients are. Read this: https://stackoverflow.com/questions/11537106/is-it-always-a-good-idea-to-store-time-in-utc-or-is-this-the-case-where-storing – Taha Paksu Nov 16 '17 at 07:26

0 Answers0