Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo Db Time format [closed]

Tags:

mongodb

I want to save the time in mongoDB inorder to add the teacher availability in a school.how can i store time of day in mongodb as a string. (I want to store like 03:30 PM) Please help me to find a solution?

like image 844
ArunJaganathan Avatar asked Mar 26 '26 13:03

ArunJaganathan


1 Answers

you can user mongoose user schema's pre function which trigger every time you added anything using the schema. inside that use conditional function how you want to save the specific schema model . i have done your time model . let me know if you have any confusion.

var mongoose = require('mongoose'),
Schema = mongoose.Schema;

var UserSchema = new Schema({
time: String
});


UserSchema.pre('save',
function (next) {
    if (!this.time) {
        var newdate = new Date();
        var Fulllocaltime =  newdate.toLocaleString('en-US');
        var array = Fulllocaltime.split(" ");
        var finaloutput = array[1]+' '+array[2];

        this.time =finaloutput;
    }
    next();
}
);
like image 118
nur farazi Avatar answered Mar 28 '26 02:03

nur farazi