2017-09-18 8 views
4

私の現在の日付はMon Jul 03 2017 10:17:40 GMT+0530(インドの標準時)です。日付のタイムゾーンをJavascriptのローカルタイムゾーンに変更するにはどうすればいいですか?

ユーザーに応じて、この日付を特定のタイムゾーンとロケールに変換する必要があります(たとえば、GMT+00:00)。

特定の日付をユーザー固有の形式に変換するにはどうすればよいですか?

+4

参照[moment.js](http://momentjs.com/)。 – Nit

+0

はい私はmoment.jsを使用しています。すなわち、 'moment(d).format(format)'しかし、どうすればそれをユーザー固有のタイムゾーン形式に変換できますか?私の場合、宛先フォーマットは固定されていません。 – krish

+0

@ krish - Dateをユーザーのタイムゾーンに変換するために何もする必要はありません。組み込みの* Date.prototype.toString *はホスト設定を使用して適切な文字列を生成します。 – RobG

答えて

-1

あなたは、ロケールに依存する日付を表示するためにMoment.js' localized formatsを使用することができ、この

var date = new Date(); 
//getTimezoneOffset() gives difference in minutes between your timezone and GMT, multiplying it by 60*1000 converts it to milliseconds 
var utc_time = new Date(date.valueOf() + date.getTimezoneOffset() * 60000); 
var offset = destination_timezone_offset_in_milliseconds; 
var time_in_required_timezone = new Date(utc_time.valueOf()+offset); 
+0

宛先タイムゾーンの形式は固定されていません。このソリューションを使用することはできません。ありがとう – krish

+0

このjavascriptはどこで実行されますか?あなたのマシンまたはユーザーのマシンで? @krish –

+0

異なるマシンと別の場所@ Shriyansh Gautam – krish

2

を試すことができます。
現在のユーザーロケールを検出するには、this questionを参照してください。

ロケールを手動でロードするか、組み込みのロケール(moment-with-locales.jsthe homepage)を使用してリリースビルドを使用する必要があります。あなたの上記の例を考えると

は:

const locale = window.navigator.userLanguage || window.navigator.language 
 
moment.locale(locale) 
 
console.log('locale: ' + moment.locale()) 
 

 
const format = 'LLLL ZZ' 
 
console.log(moment().format(format))
<script src="https://cdn.rawgit.com/moment/moment/b8a7fc31/min/moment-with-locales.min.js"></script>

+0

*言語*はロケール*ではなく、言語です。 – RobG

1

Momentjsのタイムゾーンのドキュメントには多くのサンプルがあります。

moment.tz('America/Los_Angeles').format('z') // "PDT"  (abbreviation) 
moment.tz('Asia/Magadan').format('z')   // "+11"  (3-char offset) 
moment.tz('Asia/Colombo').format('z')   // "+0530" (5-char offset) 

リンク:

https://momentjs.com/timezone/docs/

+0

ありがとうhsyn ozkara。 – krish

関連する問題