Convert UTC date time to local date time

前端 未结 30 1604
悲哀的现实
悲哀的现实 2020-11-22 01:09

From the server I get a datetime variable in this format: 6/29/2011 4:52:48 PM and it is in UTC time. I want to convert it to the current user’s browser time us

30条回答
  •  眼角桃花
    2020-11-22 01:30

    In my case, I had to find the difference of dates in seconds. The date was a UTC date string, so I converted it to a local date object. This is what I did:

    let utc1 = new Date();
    let utc2 = null;
    const dateForCompare = new Date(valueFromServer);
    dateForCompare.setTime(dateForCompare.getTime() - dateForCompare.getTimezoneOffset() * 
     60000);
    utc2 = dateForCompare;
    
    const seconds = Math.floor(utc1 - utc2) / 1000;
    

提交回复
热议问题