简体   繁体   中英

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

I get the time in the format below

How should I compare these two and find the bigger one? My Goal is to sort 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));
}

Using Regex we can extract total time first and convert that in minutes within getTimeInMinutes method and then compare them accordingly. Like this:

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'))

Another, rather verbose, option would be somthing like this: 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);

Make a function to convert the string into seconds. Here's an example:

 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);

Note: You might want to include some more checks for production if the input is generated by users. Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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