2021年1月4日星期一

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

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?

https://stackoverflow.com/questions/65572278/new-date-output-is-different-on-frontend-and-backend January 05, 2021 at 10:06AM

没有评论:

发表评论