本文介绍了将HH:mm转换成Moment.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一些时间数据,例如:

So I have some time data such as follows:

10:45 PM
11:35 PM
12:06 AM
01:34 AM

所有这些时间都在America/Los_Angeles中,并且每次都保证在该时区的今天09:00 PM之后.这样明天午夜过后的时间就会发生.

All of these times are in America/Los_Angeles and each time is guaranteed to be after 09:00 PM in today in that timezone. So that times which are past midnight take place tomorrow.

是否有一种优雅的方法可以将这些时间从当前格式转换为moment.js对象,并将时间固定在适当的日期.

Is there an elegant way to convert these times from the current format into moment.js objects with the times pinned to the appropriate day.

推荐答案

function getMomentFromTimeString(str) {
  var t = moment(str, 'HH:mm A');
  // Now t is a moment.js object of today's date at the time given in str

  if (t.get('hour') < 22) // If it's before 9 pm
    t.add('d', 1); // Add 1 day, so that t is tomorrow's date at the same time

  return t;
}

这篇关于将HH:mm转换成Moment.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 03:03