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?
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();
}
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With