How to set date always to eastern time regardless of user's time zone

前端 未结 3 1602
甜味超标
甜味超标 2020-12-10 11:56

I have a date given to me by a server in unix time: 1458619200000

NOTE: the other questions you have marked as \"duplicate\" don\'t show how to get there fro

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-10 12:01

    For dayLight saving, Eastern time become 4 hours behind UTC thats why its offset is -4x60 = -240 minutes. So when Day light is not active the offset will be -300. easternTimeOffset variable value is the key point to be noted here. Kindly see this code in action in attached image.

           var offset = new Date().getTimezoneOffset();// getting offset to make time in gmt+0 zone (UTC) (for gmt+5 offset comes as -300 minutes)
            var date = new Date();
            date.setMinutes ( date.getMinutes() + offset);// date now in UTC time
    
            var easternTimeOffset = -240; //for dayLight saving, Eastern time become 4 hours behind UTC thats why its offset is -4x60 = -240 minutes. So when Day light is not active the offset will be -300
            date.setMinutes ( date.getMinutes() + easternTimeOffset);
    

提交回复
热议问题