0

Is there a way to store Dates in local timezone and not in UTC ?

This is my schema :

var WhispSchema = new mongoose.Schema({
    text : String,
    pos : {latitude: Number, longitude: Number},
    distance : {type : Number, default : 0},
    created_by : {type : Schema.Types.ObjectId, ref : "UserSchema"},
    upvote : {type : Number, default : 0},
    downvote : {type : Number, default : 0}
});

WhispSchema.plugin(timestamps, {
  createdAt: 'created_at', 
  updatedAt: 'updated_at'
});

But the field "created_at" and "updated_at" is in UTC format and I want the local timezone.

Matheus208
  • 1,289
  • 2
  • 13
  • 26
JohnyBro
  • 341
  • 3
  • 14

1 Answers1

3

No, the BSON Date data type that's used within MongoDB to store dates is defined as a UTC datetime.

But this is really for the best, as storing datetimes in other time zones can get really messy. It's better to convert the stored UTC timestamps to a local timezone as needed after getting it from the database.

See this question regarding ways to do this within an aggregate pipeline.

Community
  • 1
  • 1
JohnnyHK
  • 305,182
  • 66
  • 621
  • 471