2017-08-08 15 views
1

UTCの日付時刻文字列とタイムゾーン名があります。私はサードパーティAPIからこれを取得しています。 JavaScript/NodeJSを使用してそのタイムゾーンの現在の時刻を取得するだけでなく、UTCの時刻をそのタイムゾーンの現地時間に変換する必要があります。同じライブラリ/メソッドを利用できますか?JavaScript:タイムゾーンを使用してUTC時刻を現地時間に変換します

var timezone = "America/New_York";//This will always be in Olson format. 
var UTCTime = "2017-09-03T02:00:00Z"; 
var localTime; //I need to find the equivalent of UTCTime for America/New_York 
var currentTime; //Current time of time zone America/New_York 
+0

役立つリソースhttps://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Date – Atiq

+0

moment(http://momentjs.com/docs/)は、非常に強力で一般的な日付操作ですほとんどの場合、あなたの要件はそれによってカバーされる可能性があります。 – tibetty

答えて

0

これは、いくつかの方法で行うことができます。

var options = { 
    timeZone: "America/New_York", 
    year: 'numeric', month: 'numeric', day: 'numeric', 
    hour: 'numeric', minute: 'numeric', second: 'numeric' 
}; 

var formatter = new Intl.DateTimeFormat([], options); 

var UTCTime = "2017-09-03T02:00:00Z"; 
var localTime = formatter.format(new Date(UTCTime)); 
var currentTime = formatter.format(new Date()); console.log(currentTime, localTime); 

それともmoment.jsライブラリを使用することができます。

0

オフセットを分単位で返すgetTimezoneOffset()を使用できます。その後、日付を対応する関数の戻り値に変更することができます。

また、moment.jsをご覧になりたい場合は、javasciptの日付については非常に便利なライブラリです。

関連する問題