0

Ok so this question has been asked here however the solutions given don't seem to resolve my issue.

I have the following:enter image description here

When I click save I refer to the these fields and create a date as shown here:

this.profile.date_of_birth = new Date(this.editProfileForm.value['year'], this.editProfileForm.value['month'], this.editProfileForm.value['day']);

Which when logged to the console reads:

Fri Dec 23 1988 00:00:00 GMT+1100 (AEDT)

I then make a call to my nodejs application which is running on http://localhost:3005, which takes the data and saves it to mongodb, before the save happens I log the value of date_of_birth as shown here:

api.put('/update', authenticate, (req, res) => {

    console.log(req.body.date_of_birth);

    // Save properties

});

however this logs:

1988-12-22T13:00:00.000Z

which is a day off....

I'm not doing any formatting before creating a new date when the user presses save, so I'm unsure into why when it leaves the front end application, and gets to the backend application the date is displayed incorrect....

The actual call to the nodejs application is here:

saveProfile(profileId: string, token: string, profile: Profile): Observable<any> {

        var body = {
            "id": profile.id,
            "date_of_birth": profile.date_of_birth,
        }


        return this.http.put(UPDATE_EDIT_PROFILE, body, {
            headers: this.setHeaders(token)
        }).map((res: any) => res.json());

    }

Can anyone recommend what could possibly going wrong?

Community
  • 1
  • 1
Code Ratchet
  • 5,758
  • 18
  • 77
  • 141

1 Answers1

0

Fri Dec 23 1988 00:00:00 GMT+1100 (AEDT) is date with the current timezone. and your server seems using UTC according to 1988-12-22T13:00:00.000Z.

you can use Date.UTC to create a Date Object with standart timezone of UTC which keeps same to your server.

var year = 1988;
var month = 12;
var day = 23;
console.log(new Date(year, month - 1, day));
console.log(new Date(Date.UTC(year, month - 1, day)));
Pengyy
  • 37,383
  • 15
  • 83
  • 73