I want to save a date
item in my database for logs of my website:
var LogSchema = new mongoose.Schema({
date: Date,
... ...
})
To generate and save a log in the back-end:
log.date = new Date()
console.log(log.date.getTime()) // it works well
log.save(...)
When I check the database in mongo, it shows: "date" : ISODate("2017-12-02T01:51:44.540Z")
.
However, in the front-end, when i want to read the log, log.date.getTime()
gives an error TypeError: log.date.getTime is not a function
. It seems that log.date
is considered as a String
rather than a Date
.
Does anyone know what's wrong here?
You're mixing up a JavaScript Date object with an ISO Date string. When you save to the database, it does become a string. You have to convert the date string back into a javascript date object before you can use the getTime() method. Try:
var myDate = new Date(dateStringFromDatabase);
console.log(myDate.getTime());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.