I have an application that I'm building that allows users to enter records on a webpage. I want to display the date/time of when each record was submitted, I've tried several different methods and finally have the date/time to display in a format that I want, but it seems as though the timestamp is recorded when the server starts up instead of when the record is added. I'm using moment.js for formatting
Here is my schema
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const moment = require("moment");
var d = new Date();
var formattedDate = moment(d).format("MM-DD-YYYY, h:mm:ss a");
let codeSchema = new Schema({
code: {
type: String
},
createdAt: {
type: String,
default: formattedDate
},
},
{
collection: "codes"
}
);
module.exports = mongoose.model("Code", codeSchema);
This is how the records look, except the date & time is when I start the server instead of when the document was actually added
123456789789 12-09-2021, 12:59:33 pm