为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

莫回无 2023-05-18 11:10:56
我得到以下格式的时间我应该如何比较这两个并找到更大的一个?我的目标是对它们进行排序: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));}
查看完整描述

3 回答

?
FFIVE

TA贡献1797条经验 获得超6个赞

使用正则表达式,我们可以先提取总时间并在方法中以分钟为单位进行转换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'))


查看完整回答
反对 回复 2023-05-18
?
jeck猫

TA贡献1909条经验 获得超7个赞

另一个相当冗长的选项是这样的: 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);


查看完整回答
反对 回复 2023-05-18
?
桃花长相依

TA贡献1860条经验 获得超8个赞

创建一个将字符串转换为秒的函数。这是一个例子:


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

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



查看完整回答
反对 回复 2023-05-18
  • 3 回答
  • 0 关注
  • 161 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信