Reputation: 189
I'm trying to decompose a date I got from Firestore. Here's the source code:
exports.computeStatistics = functions.https.onRequest((request, response) => {
let validatedViolations = [];
const getValidatedViolations = db.collection("violations").where("validated", "==", true).get().then(snapshot => {
return snapshot.forEach(doc => {
validatedViolations.push({
type: doc.data().type,
date: doc.data().date.toDate()
});
})
});
getValidatedViolations.then(snapshot =>{
return console.log(validatedViolations[0].date.getMonth(),validatedViolations[0].date.getDay());
})
});
the date value is the following:
2019-11-14T00:00:00.000Z
but the outputs of the getMonth()
and getDay()
methods are respectively 10, 4
which are wrong.
What's the problem?
Thank you.
Upvotes: 0
Views: 189
Reputation: 83181
Actually, what you get is "correct", in the sense that it corresponds to your code:
As explained here, getMonth()
:
returns the month in the specified date according to local time, as a zero-based value (where zero indicates the first month of the year).
and, as explained here, getDay()
:
returns the day of the week for the specified date according to local time, where 0 represents Sunday.
....
An integer number, between 0 and 6, corresponding to the day of the week for the given date, according to local time: 0 for Sunday, 1 for Monday, 2 for Tuesday, and so on
Since, November the 14th 2019 is a Thursday you get 10 (11 - 1) and 4.
Instead of getDay()
you should use getDate()
, and you should note that with getMonth()
, zero indicates the first month of the year.
Upvotes: 2