bootstrap datetimepicker,无法将日期和时间转换为 UTC

bootstrap datetimepicker, unable to convert date and time to UTC

这个问题可能已经问过很多次了,但是我好像找不到任何相关信息,所以想知道大家可以给我一些想法。

我正在使用 bootstrap datetimepicker 创建一个工具,我遇到了以下问题,我输入的日期和时间无法通过 moment.js

转换为 UTC 时间

代码如下:

$(document).ready(function()
{
    $('#select').datetimepicker({
        format: 'ddd, MM-DD hh:mm A'
    });
     $('#select').on('dp.change', function (e) {
        $('#example').text(moment(e.date).format("ddd, MM-DD, h:mm A"));
    });

});

<div class='col-md-5'>
   <div class="form-group">
      <div class='input-group date' id='select'>
         <input type='text' class="form-control" />
            <span class="input-group-addon">
              <span class="glyphicon glyphicon-calendar"></span>
            </span>
      </div>
   </div>
</div>

<span id="example">__________</span>

选择值后,输出与您选择的相同,而不是自动转换为UTC。如果能指出我做错的地方,我们将不胜感激!

使用 moment.js,您可以将日期转换为 utc,然后将其格式化为

$('#example').text(moment(e.date).utc().format("ddd, MM-DD, h:mm A"));

现在,您正在使用 .format() 显示您选择的日期,并向其中传递您需要从日期选择器中获得的相同值。

如果您想要 UTC 格式,请不要使用 moment().format(),而是使用 moment.utc().format()

参见 moment.js 文档 here