我使用来自http://tarruda.github.io/bootstrap-datetimepicker/的bootstrap-datetimepicker
这提供了选择本地时间日期时间的选项,我无法理解的是如何在将其发送到 cgi 之前将其转换为 UTC . 我需要这样做,因为我的服务器设置在 GMT 时区,并且输入可以来自任何时区。
所以我希望用户在他的 tz 中选择时间,但将该选择转换为 gmt,然后将其发送到我的 cgi 脚本。
如果有任何其他更好的方法来解决这个问题,我也会很感激。
<script type="text/javascript">
$('#timetime').datetimepicker({
maskInput: true,
format: 'yyyy-MM-dd hh:mm',
});
</script>
它以下面代码的形式被调用
<label for="sdate" class="control-label">* Scheduled Date (UTC/GMT)</label>
<div id="timetime" class="controls">
<input id="sdate" name="sdate" type="text" placeholder="YYYY-MM-DD HH:MM"></input>
<span class="add-on">
<i data-time-icon="icon-time" data-date-icon="icon-calendar"></i>
</span>
</div>
基于电影人提供的帮助的最终答案
<script type="text/javascript">
$('#timetime').datetimepicker({
maskInput: true,
format: 'yyyy-MM-dd hh:mm',
});
$("form").submit(function(){
// Let's find the input to check
var $input = $(this).find("input[name=sdate]");
if ($input.val()) {
// Value is falsey (i.e. null), lets set a new one, i have inversed this, input should be truthy
//$input.val() = $input.val().toISOString();
var d = $input.val();
var iso = new Date(d).toISOString();
// alert(iso);
$input.val(iso);
}
});
</script>
进一步更新以在 Firefox 和 chrome 上工作
<script type="text/javascript">
$("form").submit(function(){
// Let's find the input to check
var input = $(this).find("input[name=sdate]");
if (input.val()) {
var picker = $('#timetime').data('datetimepicker');
// alert(input.val());
// alert(picker.getLocalDate().toISOString());
input.val(picker.getLocalDate().toISOString());
}
});
</script>