how to change date timezone in mongoose?

前端 未结 6 1278
太阳男子
太阳男子 2021-01-04 11:38

In model schema,

Using

updated: {
    type: Date,
    default: Date.now

In server.js

put(function(req, res) {
    v         


        
6条回答
  •  鱼传尺愫
    2021-01-04 12:28

    I'm using moment-timezone

    npm install moment-timezone
    
    const 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
    

    :) Work for me.

提交回复
热议问题