简体   繁体   English

React Moment 减去时间格式

[英]React Moment subtract time format

I have following function to calculate time between the start and the end (which are just input fields).我遵循 function 来计算开始和结束之间的时间(这只是输入字段)。

   calculateTime() {
        if(this.state.start !== '' && this.state.end !== ''){
            let time1 = moment(this.state.end, "hh:mm"); 
            let subtract = time1.subtract(this.state.start);
            let format = moment(subtract).format("hh:mm");
            console.log(format); 
            return format;
        }
        return 0;
    }

In general the calculation works.一般来说,计算是有效的。 The value gets set via a input field and the state gets updated to the entered value.该值通过输入字段设置,state 更新为输入的值。 As seen here:如此处所示:

 <table>
    <thead>
        <tr>
            <th>Date</th>
            <th>Start</th>
            <th>End</th>
            <th>Time</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <input type="date" id="start" name="trip-start"
                    min="2022-01-01" max="2023-12-31" onChange={this.handleDate} value={this.state.date} />
            </td>
            <td>
                <input type="text" onChange={this.handleStart} placeholder="hh:mm" maxLength={5} />
            </td>
            <td>
                <input type="text" onChange={this.handleEnd} placeholder="hh:mm" maxLength={5} />
            </td>
            <td>
                <p>{this.calculateTime()}</p>
            </td>
        </tr>
    </tbody>
</table>

The function for setting the start state (setting the end value is similar except a other function gets called to update the state) function 用于设置开始 state (设置结束值类似,除了调用其他 function 来更新状态)

handleStart(event:any) {
    this.setState({
        start: event.target.value
    });
}

As you can see in the pictures the values are off by 12h正如您在图片中看到的那样,值在 12 小时前关闭例子

Here it should be 13:01 instead of 01:01这里应该是 13:01 而不是 01:01

在此处输入图像描述

Here it should be 00:01 instead of 12:01这里应该是 00:01 而不是 12:01

Has anyone an idea how to fix that besides manually adding or removing the 12h?除了手动添加或删除 12h 之外,有没有人知道如何解决这个问题?

Here is the working code:这是工作代码:

  const calculateTime = () => {
if (this.state.start !== "" && this.state.end !== "") {
  let time1 = moment(this.state.end, "hh:mm");
  let time2 = moment(this.state.start, "hh:mm");

  let hoursDiff = time1.diff(time2, "hours");
  console.log("Hours:" + hoursDiff);

  let minutesDiff = time1.diff(time2, "minutes");
  console.log("Minutes:" + minutesDiff);

  // let subtract = time1.subtract(time2);
  // let format = subtract.format("hh:mm");
  // console.log(format);
  return `${hoursDiff} : ${minutesDiff}`;
}
return 0;

}; };

Now, why your code didn't worked is beacuse, you need to wrap your startDate with moment,现在,为什么你的代码没有工作是因为你需要用时刻包装你的startDate

let time2 = moment(this.state.start, "hh:mm");
let subtract = time1.subtract(time2);

when trying to do the both hour and mins.当试图做两个小时和分钟。 operation in single function it's adding locale time as well, I don't see any option to disable that.在单个 function 中操作它也在添加语言环境时间,我没有看到任何禁用它的选项。 if you find something like that, you can use your code as well.如果你发现类似的东西,你也可以使用你的代码。 I hope this helps, after asking you to edit your question multiple times.在多次要求您编辑问题后,我希望这会有所帮助。 :) :)

Also, looking at your use case, you should consider substracting the date with time, not just time https://momentjscom.readthedocs.io/en/latest/moment/04-displaying/07-difference/此外,查看您的用例,您应该考虑用时间减去日期,而不仅仅是时间https://momentjscom.readthedocs.io/en/latest/moment/04-displaying/07-difference/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM