new Date() output is different on frontend and backend?

Viewed 528

I have a variable, "today", which pulls the month, day, and year from a new Date() object, and creates a string MMDDYYYY.

This is how I implemented it on the front end:

    var today = new Date()
    console.log("today " + today)
    var dd = String(today.getDate()).padStart(2, '0');
    var mm = String(today.getMonth() + 1).padStart(2, '0');
    var yyyy = today.getFullYear();
    var newToday = mm + dd + yyyy;
    console.log("new today " + newToday)

And here is the output:

today Mon Jan 04 2021 17:58:39 GMT-0800 (PST)
new today 01042021

Ok, everything is fine, today is indeed the 4th.

Here is the backed implementation, in my Cloud Firestore scheduled function:

    var today = new Date();
    var dd = String(today.getDate()).padStart(2, '0');
    var mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
    var yyyy = today.getFullYear();
    today = mm + dd + yyyy;

Almost identical, but when I run the cloud function, the function is writing to here:

enter image description here

notice the cloud function wrote to ../01052021, but the output from the frontend means that the frontend is looking for data in ../01042021.

Why does this discrepancy between frontend and backend happen? how can I fix it so when the day changes to 01/05 on the backend, the same happens on the frontend?

0 Answers
Related