2017-12-07 8 views
2

は、私は、ユーザーが日時変換日時ローカルUTC時間にJSで/ jqueryの

<input class="form-control" name="aution_end_time" required type="datetime-local id="auction_end_time-input"> 

は、今私がやりたいものを、この「ローカルdatetimeオブジェクトは、」UTC時刻に変換入力していることを形を持っていますその値をフォームの隠れた入力に格納します。

どうすればよいですか? 要約すると、 ユーザーがフォームにdatetimeを入力すると、一度入力すると非表示の入力は同じdatetimeでUTCで更新されます。ユーザーのローカルタイムゾーンを決定する方法と、UTCに変換する方法が不明です。ありがとう!

答えて

0

これにはmoment.jsを使用することをおすすめします。

私のコードスニペットをご覧ください!私は、発表目的のための第2の入力のためにtype="hidden"type="text"nameからidに変更しました。

document.getElementById("auction_end_time-input").oninput = function() { 
 
    var datetimeLocal = document.getElementById("auction_end_time-input").value; 
 
    var datetimeUTC = moment.utc(datetimeLocal).format(); 
 
    document.getElementById("auction_end_time_utc").value = datetimeUTC; 
 
}
<!-- moment.js --> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.3/moment-with-locales.min.js"></script> 
 

 
<input class="form-control" name="aution_end_time" required type="datetime-local" id="auction_end_time-input"> 
 
<input type="text" class="form-control" id="auction_end_time_utc">

+0

あなたは答えを好きなら@Rtraderねえ、あなたもupvoteことができます - あなたが知らなかっただけの場合には。 – Felix