0

Mongoose Model:

const patientSchema = new mongoose.Schema({
    firstName: String,
    middleName: String,
    lastName: String,
    addresses: [addressSubschema],
    dateOfBirth: Date,
    files: [{ type: mongoose.Schema.Types.ObjectId, ref: 'File' }],
    policies: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Policy' }],
    claims: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Claim' }],
    authorizations: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Authorization' }],
    statements: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Statement' }],
    documents: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Document' }],
    notes: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Note' }]
}, { timestamps: true })

I'm using a React front end and a standard Node.js/Express back end. After I create a new patient, the date of birth gets stored as a string in the document like this:

1990-05-05T00:00:00.000+00:00

When I query for this file and attempt to display the date, I'm originally allowed to display the date exactly as it comes over:

<p className="card-text">{props.data.dateOfBirth}</p>

React normally won't let you print an object directly to the screen, so right off the bat I know this is a string and not a date object

That original format is not user friendly of course, so I attempted to convert the string to a date by runnin it through new Date(), but when I did so, this is the response I got: enter image description here

I'm getting converted to my timezone and the date will end up displaying wrong. In the past, I always remember Mongoose returning me a date object that I needed to convert to a string.

My main question is: How can I get Mongoose to give me date only objects? I don't want to have to worry about times with this particular date. I just need the date.

Matthew Wolman
  • 645
  • 9
  • 18
  • Does this answer your question? [What is the best way to store dates of birth in MongoDB?](https://stackoverflow.com/questions/43540083/what-is-the-best-way-to-store-dates-of-birth-in-mongodb) – eol Aug 22 '21 at 19:51

1 Answers1

0

When you call Date#toString(), the JavaScript runtime will use your Operating System' timezone. Also,

MongoDB stores dates as 64-bit integers, which means that Mongoose does not store timezone information by default.

  • 1
    To be precise: https://docs.mongodb.com/manual/reference/bson-types/#std-label-document-bson-type-date -> "... refers to the BSON Date type as the *UTC datetime*." – eol Aug 22 '21 at 20:27
  • I'm not calling Date.toString() at any point tough. Is there a way to turn that 64 bit string into a date without the timezone adjustment happening? Other than peeling apart the string and creating a date out of it. I already know how to do that, but I'm 99% sure that amount of work isn't necessary. I have a misunderstanding to some degree, and I appreciate your response, but I'm still not quite sure what my solution is. – Matthew Wolman Aug 22 '21 at 20:54
  • Even though the docs say the date is stored as a 64-bit integer, it is still coming to me as a string, directly from the Model.find({}) query. I tried running .toString() on it but I get the exact same result because it is already a string. – Matthew Wolman Aug 22 '21 at 20:56