javascript bootstrap-datetimepicker 将选定的本地日期转换为 UTC
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18052529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
bootstrap-datetimepicker convert selected local date to UTC
提问by Allan Pinto
im using bootstrap-datetimepicker from http://tarruda.github.io/bootstrap-datetimepicker/this gives the option to select the datetime in local time, what i cannot understand is how do i convert it to UTC before sending it to the cgi. I need to do this because my server is set at GMT timezone and the input can come in from any timezone.
so i would like the user to select the time in his tz but convert that selection to gmt which sending it to my cgi script.
if there is any other better way of solving this issue also i would appreciate it.
我使用来自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>
it is being called in the form in the below code
它以下面代码的形式被调用
<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>
final answer based on the help given by 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>
further update to work on both firefox and chrome
进一步更新以同时适用于 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>
回答by glade
Just use momentjs.
只需使用momentjs。
function getUTCDate() {
return moment($('#sdate').val()).utc().format('YYYY-MM-DDTHH:mm:ssZZ');
}
function getUTCDate() {
return moment($('#sdate').val()).utc().format('YYYY-MM-DDTHH:mm:ssZZ');
}
回答by Neha
<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>
回答by Thomas Dijoux
I'm facing the same issue, my datepicker convert 14/07/2015 00:00 GMT(PARIS) to 13/07/2015 22:00 UTC.
我面临同样的问题,我的日期选择器将 14/07/2015 00:00 GMT(PARIS) 转换为 13/07/2015 22:00 UTC。
I use this for a workaround as i only want to use the date and not time.
我使用它作为一种解决方法,因为我只想使用日期而不是时间。
var dt = new Date($scope.END_DATE.value);
dt.setUTCHours(1); //set to utc 1 a.m.
$scope.holidays.END_DATE= dt;
The date is stored as 14/07/2015 01:00 in database.
日期在数据库中存储为 14/07/2015 01:00。
Hope it helps
希望能帮助到你
回答by Riyas K
This is the conversion between utc to IST an vice versa https://jsfiddle.net/wmhmgrjs/6/
这是 utc 与 IST 之间的转换,反之亦然 https://jsfiddle.net/wmhmgrjs/6/
//2016-08-02 12:55:19.743-- UTC <==> 2016-08-02 6:25:19.743 PM --IST
//2016-08-02 12:55:19.743-- UTC <==> 2016-08-02 6:25:19.743 PM --IST
var date = new Date('8/02/2016 12:55:48 AM UTC'); //2016-07-29 07:02:40.323
var s=date.toString();
alert(s);
var d = new Date('8/02/2016 6:25:00 PM GMT+0530');
//var d = new Date("August 2, 2016 18:26:00");
var n = d.toUTCString();
alert(n);