Home > Software design >  Mongodb date value to javascript string how to convert?
Mongodb date value to javascript string how to convert?

Time:08-04

I logged out the value stored in one of the documents(or notes in my case) in the date key, like this

console.log(notes[0].date)
The output that I got was enter image description here

and when methods like getDate() or toDateString() didn't work, what should i be doing?

CodePudding user response:

You will need to convert it into a javascript date object using new Date("your mongodb date string");

See: https://developer.mozilla.org/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Date for more infomation

CodePudding user response:

If your dates are stored using the date data type you should manipulate them as data object in your code.

If your dates are not stored using the date data type you should migrate your data to store the date as date data type rather than string.

You then format the date, using the user’s LOCALE, only when you present it to the user.

  • Related