繁体   English   中英

如何在混合字符串和数字javascript中获得两次并比较它们

[英]how to get two times in mixed string and number javascript and compare them

我得到以下格式的时间

我应该如何比较这两个并找到更大的一个? 我的目标是对它们进行排序:

function (a, b) {
  //here is should find a way to extract a and b to find the right number to compare
  a = '16 days, 23 hours, 39 minutes';
  b = '39 minutes';
  return ((a < b) ? -1 : ((a > b) ? 1 : 0));
}

使用正则表达式,我们可以首先提取总时间并在getTimeInMinutes方法中将其转换为分钟,然后相应地比较它们。 像这样:

function(a, b) {
   function getTimeInMinutes(timestamp) {
      const regex = /(?:(\d*?) days, )?(?:(\d*?) hours, )?(?:(\d*?) minutes)?/;
      let [, days = 0, hours = 0, minutes = 0] = regex.exec(timestamp);
      days = parseInt(days, 10);
      hours = parseInt(hours, 10);
      minutes = parseInt(minutes, 10);
   
      const totalMinutes = (days * 24 * 60) + (hours * 60) + minutes;
      return totalMinutes;
   }

   const totalMinutesInA = getTimeInMinutes(a);
   const totalMinutesInB = getTimeInMinutes(b);
   
   return totalMinutesInA - totalMinutesInB;
   
}

 function compare(a, b) { function getTimeInMinutes(timestamp) { const regex = /(?:(\\d*?) days, )?(?:(\\d*?) hours, )?(?:(\\d*?) minutes)?/; let [, days = 0, hours = 0, minutes = 0] = regex.exec(timestamp); days = parseInt(days, 10); hours = parseInt(hours, 10); minutes = parseInt(minutes, 10); const totalMinutes = (days * 24 * 60) + (hours * 60) + minutes; return totalMinutes; } const totalMinutesInA = getTimeInMinutes(a); const totalMinutesInB = getTimeInMinutes(b); console.log({totalMinutesInA, totalMinutesInB}) return totalMinutesInA - totalMinutesInB; } console.log('Test 1', compare('4 minutes', '3 minutes')); console.log('Test 2', compare('24 minutes', '1 hours, 3 minutes')); console.log('Test 2', compare('2 days, 24 minutes', '2 days, 24 minutes'))

另一个相当冗长的选项是这样的: https : //jsfiddle.net/0k9asxeb/

var a = '16 days, 23 hours, 39 minutes';
var b = '39 minutes';

const getDayMinutes = (string) => {
  if (!string.includes(' days')) {
    return 0;
  }

  const days = string.split(' days')[0];
  console.log(days);

  return days * 24 * 60;
}

const getHoursMinutes = (string) => {
  if (!string.includes (' hours')) {
    return 0;
  }

  const hoursDirty = string.split(' hours')[0];
  console.log(hoursDirty);

  if (!hoursDirty.includes(',')) {
    return hoursDirty;
  }

  const hours = hoursDirty.slice(hoursDirty.lastIndexOf(',') + 2, hoursDirty.length);
  console.log(hours);

  return hours * 60;
}

const getMinutes = (string) => {
  if (!string.includes(' minutes')) {
    return 0;
  }

  const minutesDirty = string.split(' minutes')[0];
  console.log(minutesDirty);

  if (!minutesDirty.includes(',')) {
    return minutesDirty;
  }

  const minutes = minutesDirty.slice(minutesDirty.lastIndexOf(',') + 2, minutesDirty.length);
  console.log(minutes);

  return minutes;
}

const getTotalMinutesInString = (string) => {
  const dayMinutes = getDayMinutes(string);
  console.log('dayMinutes', dayMinutes);

  const hoursMinutes = getHoursMinutes(string);
  console.log('hoursMinutes', hoursMinutes);

  const minutes = getMinutes(string);
  console.log('minutes', minutes);

  return parseInt(dayMinutes + hoursMinutes + minutes);
}

const totalMinutes = getTotalMinutesInString(b);
console.log(totalMinutes);

制作一个将字符串转换为秒的函数。 下面是一个例子:

 const toTimeConvertor = (timeEntry) => { const string = timeEntry.replace(',', ''); switch(string) { case 'day': case 'days': return 60 * 60 * 24; case 'hour': case 'hours': return 60 * 60; case 'minute': case 'minutes': return 60; case 'second': case 'seconds': return 1; default: return 0; } }; const stringToSeconds = (timeString) => { return [...timeString.split(',')].reduce((seconds, entry) => { const values = entry.trim().split(' '); const timeNumberString = values[0]; const timeConvertorString = values[1]; const timeNumber = Number.parseInt(timeNumberString); const timeConvertor = toTimeConvertor(timeConvertorString) const time = timeNumber * timeConvertor; return seconds + time; }, 0); }; const myFilter = (a, b) => stringToSeconds(a) - stringToSeconds(b); //Demo Output const list = [ '16 days, 23 hours, 39 minutes', '39 minutes', '5 days, 36 minutes' ]; const sortedList = list.sort(myFilter); console.log(list);

注意:如果输入是由用户生成的,您可能希望包含更多的生产检查。 祝你好运。

暂无
暂无

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

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