Javascript MomentJs wrong output

2019-07-09 04:23发布

1.

Ok, for the first question I have a question about Moment. It doesn't calculate the time right(I think?) This is my script:

function CalculateTime() {
    try {
        var originalStartTime = $('#startTime').val(),
            originalEndTime   = $('#endTime').val(),
            originalBreakTime = $('#breakTime').val();

        // breaking hours and minutes. needed format: HH:mm
        var startHours   = originalStartTime.substring(0, 2).replace(':', ''),
            startMinutes = originalStartTime.substring(3, 5),
            endHours     = originalEndTime.substring(0, 2).replace(':', ''),
            endMinutes   = originalEndTime.substring(3, 5);

        // momentJs variables
        var mStart = moment().hour(startHours).minute(startMinutes),
            mEnd = moment().hour(endHours).minute(endMinutes),
            mBreak = moment.duration(originalBreakTime);


        var result = moment( mEnd - mStart - mBreak).format('HH:mm');

        $('#workedHours').val(result + " timmar");

    } catch (err) {
        $('#workedHours').val(err);
    }  

startTime: 07:00, endTime: 16:00, breakTime 00.00 That will result with workedHours being 10.00. This is from the chrome console: enter image description here

1条回答
我只想做你的唯一
2楼-- · 2019-07-09 05:13

You need to use the subtract method

function CalculateTime() {
  try {
    var originalStartTime = $('#startTime').val(),
      originalEndTime = $('#endTime').val(),
      originalBreakTime = $('#breakTime').val();

    // breaking hours and minutes. needed format: HH:mm
    var startHours = originalStartTime.substring(0, 2).replace(':', ''),
      startMinutes = originalStartTime.substring(3, 5),
      endHours = originalEndTime.substring(0, 2).replace(':', ''),
      endMinutes = originalEndTime.substring(3, 5),
      breakHours = originalBreakTime.substring(0, 2).replace(':', ''),
      breakMinutes = originalBreakTime.substring(3, 5);

    // momentJs variables
    var mStart = moment().hour(startHours).minute(startMinutes),
      mEnd = moment().hour(endHours).minute(endMinutes),
      mBreak = moment.duration(breakHours * 60 + +breakMinutes, 'minutes');

    var result = mEnd.subtract(mStart).subtract(mBreak).format('HH:mm');

    $('#workedHours').val(result + " timmar");

  } catch (err) {
    $('#workedHours').val(err);
  }
}

CalculateTime();
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="startTime" value="07:00" />
<input id="endTime" value="16:00" />
<input id="breakTime" value="00.30" />
<input id="workedHours" />


I think a more momentjs way is

function CalculateTime() {
  try {
    var originalStartTime = $('#startTime').val(),
      originalEndTime = $('#endTime').val(),
      originalBreakTime = $('#breakTime').val();

    // momentJs variables
    var mStart = moment(originalStartTime, 'HH:mm'),
      mEnd = moment(originalEndTime, 'HH:mm'),
      mBreak = moment.duration(originalBreakTime);

    var result = mEnd.subtract(mStart).subtract(mBreak).format('HH:mm');

    $('#workedHours').val(result + " timmar");

  } catch (err) {
    $('#workedHours').val(err);
  }
}

CalculateTime();
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.6/moment.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<input id="startTime" value="07:00" />
<input id="endTime" value="16:00" />
<input id="breakTime" value="00:30" />
<input id="workedHours" />

查看更多
登录 后发表回答