return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - Convert datetime-local to UTC time in jsjquery - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert datetime-local to UTC time in jsjquery - Stack Overflow

programmeradmin0浏览0评论

I have a form that a user can input a datetime

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

Now what I would like to do is convert this "local datetime object" to UTC time and store that value in a hidden input of the form.

<input type="hidden" class="form-control" name="auction_end_time_utc">

How would I go about this? To summarize: user enters a datetime in the form, then once they do that the hidden input gets updated with the same datetime but in UTC. I am unsure of how to determine the users local time zone and how to convert to UTC. Thanks!

I have a form that a user can input a datetime

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

Now what I would like to do is convert this "local datetime object" to UTC time and store that value in a hidden input of the form.

<input type="hidden" class="form-control" name="auction_end_time_utc">

How would I go about this? To summarize: user enters a datetime in the form, then once they do that the hidden input gets updated with the same datetime but in UTC. I am unsure of how to determine the users local time zone and how to convert to UTC. Thanks!

Share Improve this question asked Dec 7, 2017 at 22:21 RtraderRtrader 9475 gold badges11 silver badges27 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I would remend to use moment.js for that.

See my code snippet! I changed type="hidden" to type="text" and name to id for the second input for presentational purpose.

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./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">

Or one line of javascript:

const inputEl = document.getElementByName('aution_end_time');

const isoDatetimeStr = (new Date(inputEl.value)).toISOString();
发布评论

评论列表(0)

  1. 暂无评论