Angular 4 date pipe displays wrong date because of time zones - how to fix this?

前端 未结 3 1077
[愿得一人]
[愿得一人] 2020-11-29 03:24

I have a date value in each of my objects that I can Print like this:

 {{competition.compStart }}

And here is how it l

3条回答
  •  暖寄归人
    2020-11-29 03:51

    I also faced the same problem. before saving data convert the date format to the one used in service.For example in my case in Java it was 'YYYY-MM-DD'.So in angular just before saving all the data: where birthdate is binded to your template field or is simply displayed.

    this.birthdate= this.datePipe.transform(birthDate, 'yyyy-MM-dd'); where birthdate is not a date object but a string date.

    Now, when you need to show the date on the UI use:

                    let dd = (new Date(data.birthDate)).getUTCDate().toString();
    
                    let mm = ((new Date(data.birthDate)).getMonth()).toString();
                    let yy = (new Date(data.birthDate)).getFullYear().toString();
    
                    this.birthDate = new Date(Number(yy), Number(mm), Number(dd));
    

提交回复
热议问题