我使用的是http://tarruda.github.io/bootstrap-datetimepicker/的bootstrap-datetimepicker,它提供了在本地时间选择日期时间的选项,我不能理解的是在发送到cgi之前如何将其转换为UTC。我需要这样做,因为我的服务器设置为GMT时区,输入可以来自任何时区。
所以我希望用户在他的tz中选择时间,但将选择转换为gmt,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>最终答案基于filmor提供的帮助
<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>发布于 2014-03-19 00:26:14
只要使用momentjs即可。
http://momentjs.com/
function getUTCDate() { return moment($('#sdate').val()).utc().format('YYYY-MM-DDTHH:mm:ssZZ'); }
发布于 2013-08-05 21:11:26
<script type="text/javascript">
$('#timetime').datetimepicker({
maskInput: true,
format: 'yyyy-MM-dd hh:mm',
});
$("form").submit(function(){
var input = $('#sdate').val();
var input = convertToUtc(input);
});
function convertToUtc(str) {
var date = new Date(str);
var year = date.getUTCFullYear();
var month = date.getUTCMonth()+1;
var dd = dategetUTCDate();
var hh = date.getUTCHours();
var mi = date.getUTCMinutes();
var sec = date.getUTCSeconds();
// 2010-11-12T13:14:15Z
theDate = year + "-" + (month [1] ? month : "0" + month [0]) + "-" +
(dd[1] ? dd : "0" + dd[0]);
theTime = (hh[1] ? hh : "0" + hh[0]) + ":" + (mi[1] ? mi : "0" + mi[0]);
return [ theDate, theTime ].join("T");
}
</script>发布于 2015-07-15 17:46:33
我也面临着同样的问题,我的datepicker将14/07/2015 00:00 GMT(巴黎)转换为13/07/2015 22:00 UTC。
我将此作为一个变通方法,因为我只想使用日期,而不是时间。
var dt = new Date($scope.END_DATE.value);
dt.setUTCHours(1); //set to utc 1 a.m.
$scope.holidays.END_DATE= dt;日期在数据库中存储为14/07/2015 01:00。
希望能有所帮助
https://stackoverflow.com/questions/18052529
复制相似问题