如何根据测试用例格式化

时间:2019-01-28 15:27:05

标签: javascript time

  var years = Math.floor(seconds / (3600*24*365))
  seconds -= years*3600*24*365
  var days = Math.floor(seconds / (3600*24))
  seconds  -= days*3600*24
  var hrs   = Math.floor(seconds / 3600)
  seconds  -= hrs*3600
  var minutes = Math.floor(seconds / 60)
  seconds  -= minutes*60



console.log(formatDuration(3662)); // 1 hour, 1 minute and 2 second
console.log(formatDuration(62)); // 1 minute and 2 second
console.log(formatDuration(94608000)); // 3 years

如何像这样console.log?在此先感谢。

如果只有1个实体,则没有昏迷,并且

如果有2个实体使用和

如果3使用昏迷和

2 个答案:

答案 0 :(得分:2)

手动有点冗长,但是当然可以做到:

function formatDuration(seconds) {
    let years = Math.floor(seconds / (3600 * 24 * 365));
    seconds -= years * 3600 * 24 * 365;
    let days = Math.floor(seconds / (3600 * 24));
    seconds -= days * 3600 * 24;
    let hrs = Math.floor(seconds / 3600);
    seconds -= hrs * 3600;
    let minutes = Math.floor(seconds / 60);
    seconds -= minutes * 60;
    let result = '';
    // Format the string based on the various time frames
    if (years > 0) {
        if (years == 1)
            result += years + ' year,';
        else
            result += years + ' years,';
    }
    if (days > 0) {
        if (days == 1)
            result += days + ' day,';
        else
            result += days + ' days,';
    }
    if (hrs > 0) {
        if (hrs == 1)
            result += hrs + ' hour,';
        else
            result += hrs + ' hours,';
    }
    if (minutes > 0) {
        if (minutes == 1)
            result += minutes + ' minute,';
        else
            result += minutes + ' minutes,';
    }
    if (seconds > 0) {
        if (seconds == 1)
            result += seconds + ' second,';
        else
            result += seconds + ' seconds,';
    }
    // Remove the comma at the end
    result = result.slice(0, result.length - 1);
    // find the index of the last comma
    let n = result.lastIndexOf(',');
    // Divide the string where the comma was and substitute with an and
    result = result.slice(0, n) + result.slice(n).replace(',', ' and ');
    return result;
}

console.log(formatDuration(3662)); // 1 hour, 1 minute and 2 second
console.log(formatDuration(62)); // 1 minute and 2 second
console.log(formatDuration(94608000)); // 3 years

我建议使用时间库,查看Moment.js是那里最好的时间之一

答案 1 :(得分:1)

我已经在您的三个示例中检查了以下代码,一切正常。它还会检查结尾的 s

function formatTime(seconds){
  var years = Math.floor(seconds / (3600*24*365))
  seconds -= years*3600*24*365
  var days = Math.floor(seconds / (3600*24))
  seconds  -= days*3600*24
  var hrs   = Math.floor(seconds / 3600)
  seconds  -= hrs*3600
  var minutes = Math.floor(seconds / 60)
  seconds  -= minutes*60
  let obj = {};
  //Add the values only if they are not 0
  if(years >= 1) obj.years = years;
  if(days >= 1) obj.days = days;
  if(hrs >= 1) obj.hrs = hrs;
  if(minutes >= 1) obj.minutes = minutes;
  if(seconds >= 1) obj.seconds = seconds;
  //get keys of obj
  let keys = Object.keys(obj); 
  let str = ''; //String which will be returned
  for(let i = 0;i<keys.length;i++){
      let key = keys[i]
      let value = obj[key];
      //remove the ending 's' if value is 1
      key = (value > 1) ? key:key.substring(0,key.length - 1);
      //add end before the last value
      if(i === keys.length - 1 && keys.length != 1) str += ' and ';
      //for all value other than last value a ',' is added before them
      else str += ','

      str += `${value} ${key}`;
  }
  return str.substring(1,str.length);   // removes the starting comma which is added in above loop
}