問題描述
我有一個服務器在 Unix 時間給我的日期:1458619200000
I have a date given to me by a server in unix time: 1458619200000
注意:您標記為重復"的其他問題并未顯示如何從 UNIX TIME 到達那里.我正在尋找 javascript 中的特定示例.
但是,我發現根據我的時區,我會得到兩種不同的結果:
However, I find that depending on my timezone I'll have two different results:
d = new Date(1458619200000)
Mon Mar 21 2016 21:00:00 GMT-0700 (Pacific Daylight Time)
//現在我將計算機設置為東部時間,我得到了不同的結果.
// Now I set my computer to Eastern Time and I get a different result.
d = new Date(1458619200000)
Tue Mar 22 2016 00:00:00 GMT-0400 (Eastern Daylight Time)
那么我如何顯示日期:1458619200000 ... 無論我的計算機的時區如何,始終處于東部時間(3 月 22 日)?
So how can I show the date: 1458619200000 ... to always be in eastern time (Mar 22) regardless of my computer's time zone?
推薦答案
您可以使用 getTimezoneOffset() 函數.例如,
You can easily take care of the timezone offset by using the getTimezoneOffset() function in Javascript. For example,
var dt = new Date(1458619200000);
console.log(dt); // Gives Tue Mar 22 2016 09:30:00 GMT+0530 (IST)
dt.setTime(dt.getTime()+dt.getTimezoneOffset()*60*1000);
console.log(dt); // Gives Tue Mar 22 2016 04:00:00 GMT+0530 (IST)
var offset = -300; //Timezone offset for EST in minutes.
var estDate = new Date(dt.getTime() + offset*60*1000);
console.log(estDate); //Gives Mon Mar 21 2016 23:00:00 GMT+0530 (IST)
雖然,后面表示的語言環境字符串不會改變.此答案的來源在這篇文章中.希望這會有所幫助!
Though, the locale string represented at the back will not change. The source of this answer is in this post. Hope this helps!
這篇關于無論用戶的時區如何,如何始終將日期設置為東部時間的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!