Change ISO Date String to Date Object - JavaScript

前端 未结 8 1723
花落未央
花落未央 2021-01-01 08:54

I am stuck in a weird situation and unfortunately,even after doing some RnD and googling, i am unable to solve this problem.

I have a date string in ISO format, lik

8条回答
  •  耶瑟儿~
    2021-01-01 09:34

    new Date(this.datePipe.transform(isoDate,'yyyy-MM-dd HH:mm:ss', 'UTC'));
    

    Angular datepipe transform will convert it to a simple date time format as specified with no timezone. In case of ISO string format, the date constructor would have treated it as UTC time, but now, the Date constructor treats it as the local time.

    Example in my case(IST):

    input string: 2020-04-19T09:15:00.000Z
    after transform this.datePipe.transform(input string)
    2020-04-19 09:15:00
    Date object 
    new Date(this.datePipe.transform(isoDate,'yyyy-MM-dd HH:mm:ss', 'UTC'));//treats the transformed date as local timezone date when creating the date object
    Sun Apr 19 2020 09:15:00 GMT+0530 (India Standard Time)
    

提交回复
热议问题