【问题标题】:Generating Duration from Time Values从时间值生成持续时间
【发布时间】:2022-01-04 14:09:56
【问题描述】:

我正在尝试通过moment.js 找到一种方法来转换两个时间值,它们以开始和停止时间的形式出现,以小时和分钟表示为字符串值,如下所示:开始:1:12,和停止:2:10,或者在下午,开始:14:23,然后停止:15:22 并由此生成以毫秒为单位的持续时间。我尝试了以下方法,但它给了我一个NaN 结果:

澄清一下,我收到的是字符串格式的数据。如下所示:

const startValue = "1:12";
const stopValue = "2:10"; // for a duration of 58 minutes

const msDuration = moment(stopValue).format('HH:mm').valueOf() - moment(startValue).format('HH:mm').valueOf();

我认为这里的问题是我的startValuestopValue 需要先转换为不同的格式,因为它们当前是字符串值?那么,如何将“1:12”的字符串值转换为moment 可以使用的值?

【问题讨论】:

  • 不要格式化为字符串。
  • 您的解析格式错误。看看moment parses是什么。
  • @DanielA.White,哈哈,我别无选择。这就是我收到数据的方式。
  • @DanielA.White moment.js 实际上可以格式化字符串,但不能格式化那种字符串。例如支持 ISOStrings
  • 那么,@MrJami,那么正确的格式是什么?这就是我问题的根源。像这样的东西? moment(new Date(stopTime).format('HH:mm').valueOf())?还是我还需要传递一些类似日期的信息?

标签: javascript momentjs


【解决方案1】:

你的代码有两个问题:

  1. 解析无效

    您的时间输入不是 Moment 识别的标准格式,因此您必须使用格式化的解析来向它解释您的时间。

  2. formatting 作为调用前的字符串 valueOf.

    .format() 将你的 Moment 对象转换为字符串,字符串不能相减。要解决这个问题,您可以直接拨打 .format() 电话。相反,您也可以使用.diff(),它会为您计算差异。

因此,您可以使用以下任何一种:

const msDuration = moment(stopValue, "H:mm").valueOf() - moment(startValue, "H:mm").valueOf()
const msDuration = moment(stopValue, "H:mm") - moment(startValue, "H:mm") // Subtracting will implicitly call .valueOf()
const msDuration = moment(stopValue, "H:mm").diff(moment(startValue, "H:mm"))

【讨论】:

  • 谢谢,这是我希望得到的简洁答案。
【解决方案2】:

根据您的上述 cmets,我知道您收到的时间是以下格式的响应:“HH:mm”。

如果实际日期并不重要,那么您可以像这样转换它 (JsFiddle):

const startValue = "1:12";
const stopValue = "2:10"; // for a duration of 58 minutes
const start = { hour: startValue.split(":")[0], minute: startValue.split(":")[0] }
const stop = { hour: stopValue .split(":")[0], minute: stopValue .split(":")[0] }

const msDuration = moment().hours(stop.hour).minutes(stop.minute).valueOf() - moment().hours(start.hour).minutes(start.minute).valueOf(); //you might get 1 ms less. so jest set also ms to 0
const msDurationA = moment().hours(stop.hour).minutes(stop.minute).milliseconds(0).valueOf() - moment().hours(start.hour).minutes(start.minute).milliseconds(0).valueOf();

所以基本上你正在获取当前日期并改变小时和分钟,所以你可以使用 moment.Js。

虽然我的问题是,你为什么需要时刻?你也可以用原生 JS 来做:

const startValue = "1:12";
const stopValue = "2:10"; // for a duration of 58 minutes

const start = { hour: +startValue.split(":")[0], minute: +startValue.split(":")[1] };
const stop = { hour: +stopValue.split(":")[0], minute: +stopValue.split(":")[1] };

const msDuration = (stop.hour * 3600000 + stop.minute * 60000) - (start.hour * 3600000 + start.minute * 60000);

console.log("msDuartion: ", msDuration);

【讨论】:

    【解决方案3】:

    使用原生 JS:

    function getMilliSecs(text) {
      const spl = text.split(":");
      return parseInt(spl[0])*60*60*1000 + parseInt(spl[1])*60*1000; 
    }
    
    function getMilliSecsDuration(start, stop) {
      return getMilliSecs(stop) - getMilliSecs(start);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 2020-09-24
      • 2018-05-06
      • 2012-01-09
      • 2023-04-05
      • 2015-08-31
      • 1970-01-01
      相关资源
      最近更新 更多