JohnyBro
JohnyBro

Reputation: 361

MongoDB date in timezone

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.

Upvotes: 0

Views: 5437

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 312129

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.

Upvotes: 3

Related Questions