使用 moment 将对象中的本地时间转换为 UTC

Convert local time in an object to UTC using moment

考虑以下对象。

time: {
  hour: 3, 
  minute: "03", 
  hourType: "AM"
}

考虑到日期是使用时刻的当前日期,是否可以将上述时间转换为 UTC。

您可以使用 moment.utc with object 参数。

由于AM/PM没有key所以需要自己管理

这是一个活生生的例子:

var time = {
  hour: 3, 
  minute: "03", 
  hourType: "AM"
}
var m = moment.utc({
  hour: time.hourType=='AM' ? time.hour : 12 + time.hour,
  minutes: time.minute
});
console.log(m.format());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>