Convert UTC date time to local date time

前端 未结 30 1155
悲哀的现实
悲哀的现实 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:48

    This is what I'm doing to convert UTC to my Local Time:

    const dataDate = '2020-09-15 07:08:08'
    const utcDate = new Date(dataDate);
    const myLocalDate = new Date(Date.UTC(
       utcDate.getFullYear(),
       utcDate.getMonth(),
       utcDate.getDate(),
       utcDate.getHours(),
       utcDate.getMinutes()
    ));
    
    document.getElementById("dataDate").innerHTML = dataDate; 
    document.getElementById("myLocalDate").innerHTML = myLocalDate; 

    UTC

    Local(GMT +7)

    Result: Tue Sep 15 2020 14:08:00 GMT+0700 (Indochina Time).

提交回复
热议问题