将自定义持续时间字符串转换为时刻持续时间以进行比较

Convert custom duration string to moment duration for comparison

对于 table 中的自定义排序函数,我需要一个可以比较两个持续时间字符串的函数。这些是自定义格式。

这是我用来将毫秒转换为字符串的函数。 seconds 布尔值在那里,所以如果需要我可以忽略秒部分。

export function convertMillisecondsToHours(
  milliseconds: number,
  seconds: boolean
): string {
  if (seconds)
    return `${moment
      .duration(milliseconds)
      .format('d [d] hh [h] mm [min] ss [s]')}`;
  else
    return `${moment.duration(milliseconds).format('d [d] hh [h] mm [min]')}`;
}

结果字符串如下所示,例如:1 d 21 h 59 mins.

现在,在我的 table(PrimeNG,但这并不重要)中,我需要一个自定义排序函数,它能够将这种持续时间从最短到最长排序。我的想法是将其转换为 moment.js 日期或持续时间和 diff/subtract。这是我得到的(在转换部分失败):

 customSort(event: SortEvent) {
    console.log(`Sorting`)
    event.data.sort((data1, data2) => {
      let value1: string = data1[event.field];
      let value2: string = data2[event.field];

      // Convert to moment date/duration, compare (subtract/diff) and return value.
      const date1 = moment.duration(value1)
      const date2 = moment.duration(value2)
      return (event.order * date2.subtract(date1).asMilliseconds());
    });
  }

转换后的日期目前无效。如何将我的自定义持续时间字符串转换回我可以在比较函数中使用的日期或 unix 时间戳?

最后,我写了自己的函数。

/**
 * Converts a duration string from the table to milliseconds.
 * @param durationString The duration string in format d [d] hh [h] mm [min] ss [s] or d [d] hh [h] mm [min]
 */
export function customDurationStringToMilliseconds(durationString: string): number {
  let duration = 0;

  // Add days.
  if (durationString.includes('d')) {
    duration +=
      parseInt(durationString.substr(0, durationString.indexOf('d')), 10) *
      86400;
    durationString = durationString.replace(
      durationString.substring(0, durationString.indexOf('d') + 1),
      ''
    );
  }

  // Add hours.
  if (durationString.includes('h')) {
    duration +=
      parseInt(durationString.substr(0, durationString.indexOf('h')), 10) *
      3600;
    durationString = durationString.replace(
      durationString.substring(0, durationString.indexOf('h') + 1),
      ''
    );
  }

  // Add minutes.
  if (durationString.includes('min') || durationString.includes('mins')) {
    duration +=
      parseInt(durationString.substr(0, durationString.indexOf('min')), 10) *
      60;
    if (durationString.includes('mins'))
      durationString = durationString.replace(
        durationString.substring(0, durationString.indexOf('mins') + 4),
        ''
      );
    else
      durationString = durationString.replace(
        durationString.substring(0, durationString.indexOf('min') + 3),
        ''
      );
  }

  // Add seconds.
  if (durationString.includes('s')) {
    duration += parseInt(
      durationString.substr(0, durationString.indexOf('s')),
      10
    );
  }
  return duration;
}

可能不是最优雅的解决方案,但它确实有效。排序:

export function customSort(event: SortEvent) {
    event.data.sort((data1, data2) => {
          const value1: string = data1[event.field];
          const value2: string = data2[event.field];
          return (
            event.order *
            (customDurationStringToMilliseconds(value1) -
            customDurationStringToMilliseconds(value2))
        );
    });
}