2016-10-05 52 views
0

我需要太平洋時間美國&加拿大在我的JavaScript代碼。我使用下面的代碼行對於moment.js格式當前日期時間在多個時區

moment(new Date()).zone("-07:00").format('hh:mm A') 

,但如果白天是那麼靜-07:00將給出不正確的輸出

如何獲得使用moment.js的JavaScript當前服務器時間?

回答

2

你沒有說明你想要的加拿大時區,但是moment.js的timezone add on可能是你需要的。這樣,您可以這樣做下面這對於DST而不是DST內部支持:

var pacific = moment.tz("US/Pacific"); 
 
var canada = pacific.clone().tz("Canada/Eastern"); 
 

 
console.log(pacific.format('hh:mm A')); 
 
console.log(canada.format('hh:mm A'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.min.js"></script> 
 
<script src="http://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.4.1/moment-timezone-with-data-2010-2020.min.js"></script>

+1

非常感謝DelightedD0D讚賞。 –

相關問題