how to change date timezone in mongoose? how to change date timezone in mongoose? mongoose mongoose

how to change date timezone in mongoose?


I'm using moment-timezone

npm install moment-timezoneconst moment = require('moment-timezone');const dateThailand = moment.tz(Date.now(), "Asia/Bangkok");console.log(dateThailand); // "2018-08-20T16:35:14.033+07:00"*** Asia/Bangkok +07:00

Schema in the mongoose.

const categorySchema = new Schema(    {        _id: {type: mongoose.Schema.Types.ObjectId, auto: true},        c_name: String,        created_by: String,        created_date: {type: Date, default: dateThailand},        updated_by: String,        updated_date: {type: Date, default: dateThailand}    }, {_id: false});

See up that created_date, updated_date: {type: Date, default: dateThailand }

Read more: http://momentjs.com/timezone/docs/

*If you using Robo 3T tool.

You can set "Display Dates In..."

Options > Display Dates In... > Local Timezone

enter image description here

:) Work for me.


The timestamps are timezone agnostic, stored as a unix timestamp. This timestamp will work across timezones, and node interprets it using current timezone of the server. The date you've shown is correctly stored. As soon as you'll retrieve it, if your server's timezone is UTC+2, it will show you correct time.


There is nothing wrong in your code. MongoDb saves date in UTC format no matter in whichever timezone you try to insert your date.

If you log domain.updated before saving in DB, result will be UTC+2 (your local time)

If you see updated column in DB, result will be in UTC

If you fetch updated column value from DB, then again result will be in UTC+2 (your local time)