如何添加逗号和"和"在正确的地方

时间:2016-08-16 20:15:46

标签: javascript date time formatting human-readable

输入:秒,输出:语法正确,格式化时间(拼出单词)。

到目前为止,我设法从说400443731 year, 98 days, 5 hours, 37 minutes, 1 second - 请注意正确的复数和逗号。

我缺少的是"和"这是添加而不是最后一个逗号(当然只有在有足够的输入时)。例如1 year and 1 second用于输入31556953,而不是我当前的1 year, 1 second

function formatDuration (seconds) {

var numyears = Math.floor(seconds / 31556952);
if (numyears > 1) {var pluryears = " years, "} else {var pluryears = " year, "};
if (numyears > 0) {var printyears = numyears + pluryears;} else {var printyears = ''};

var numdays = Math.floor((seconds % 31556952) / 86400);
if (numdays > 1) {var plurdays = " days, "} else {var plurdays = " day, "};
if (numdays > 0) {var printdays = numdays + plurdays;} else {var printdays = ''};

var numhours = Math.floor(((seconds % 31556952) % 86400) / 3600);
if (numhours > 1) {var plurhours = " hours, "} else {var plurhours = " hour, "};
if (numhours > 0) {var printhours = numhours + plurhours;} else {var printhours = ''};

var numminutes = Math.floor((((seconds % 31556952) % 86400) % 3600) / 60);
if (numminutes > 1) {var plurminutes = " minutes, "} else {var plurminutes = " minute, "};
if (numminutes > 0) {var printminutes = numminutes + plurminutes;} else {var printminutes = ''};

var numseconds = (((seconds % 31556952) % 86400) % 3600) % 60;
if (numseconds > 1) {var plurseconds = " seconds"} else {var plurseconds = " second"};
if (numseconds > 0) {var printseconds = numseconds + plurseconds;} else {var printseconds = ''};

return(printyears + printdays + printhours + printminutes + printseconds)
}

formatDuration(31556953);

5 个答案:

答案 0 :(得分:3)

更紧凑的解决方案,按预期输出时间字符串:

function formatDuration (seconds) {
  var values = {
    years: Math.floor(seconds / 31556952),
    days: Math.floor((seconds % 31556952) / 86400),
    hours: Math.floor(((seconds % 31556952) % 86400) / 3600),
    minutes: Math.floor((((seconds % 31556952) % 86400) % 3600) / 60),
    seconds: (((seconds % 31556952) % 86400) % 3600) % 60,
  };
  var withUnits = Object.keys(values)
  .filter(function(unit) { return values[unit] > 0; })
  .map(function (unit) {
    var value = values[unit];
    return value + ' ' + (value === 1 ? unit.slice(0, -1) : unit);
  });
  return (withUnits.length > 1 ? withUnits.slice(0, -1).join(', ') + ' and ' : '') + withUnits.pop();
}

console.log(formatDuration(40044373));

答案 1 :(得分:1)

只是一个小正则表达式替换了最后一个逗号:

return (printyears + printdays + printhours + printminutes + printseconds)
    .replace(/, (.*)$/, " and $1");

答案 2 :(得分:1)

无关,但提供的代码很难想到。

我将复数位提取到它自己的方法中,粗略地说:

function pluralize(dur, s) {
  var ret = false;

  if (dur > 0) {
    ret = dur + ' ' + s;

    if (dur > 1) {
      ret += 's';
    }
  }

  return ret;
}

然后主线代码,而不是手动完成所有操作,推送到数组,例如,

function formatDuration(seconds) {
  var segments = [];

  var years  = Math.floor(seconds / 31556952)
    , sYears = pluralize(years, 'year')
    ;

  if (sYears) { 
    segments.push(sYears); 
  }

  // etc.

您只需要特殊情况seconds值:

  var seconds = (((seconds % 31556952) % 86400) % 3600) % 60
    , sSeconds = pluralize(seconds, 'second')
    ;

  var tmp = segments.join(', ');
  if (!sSeconds) {
    return tmp;
  }

  return tmp + ' and ' + sSeconds;
}

这可以进一步清理,但产生如下输出:

1 year, 2 hours, 46 minutes and 43 seconds
1 year and 3 seconds

(虽然我更喜欢牛津逗号。)

我还会做的事情:

  • 停止重新计算一切;保持剩余秒数的计数。
  • 将数组传递给函数,避免在主线代码中进行手工操作。
    • (或以其他方式包装。)

https://gist.github.com/davelnewton/a1371867527c5f1530498e1555e2fb0a

答案 3 :(得分:1)

为了这个目的,我写了一个小型图书馆:

https://github.com/adamshaylor/compound-subject

在您的情况下,您可以使用它:

var formattedTimeString = compoundSubject([
    numyears + 'years',
    numdays + 'days',
    numhours + 'hours',
    numminutes + 'minutes',
    numseconds + 'seconds'
]).make();

产生类似的东西:

  

2年,5天,3小时,10分钟和2秒

您也可以致电delimitAll()添加牛津逗号。

答案 4 :(得分:0)

您只需要添加'和'在你的回归。

return(printyears + printdays + printhours + printminutes + 'and' + printseconds)

并在'分钟后删除逗号,'在这一行

if (numminutes > 1) {var plurminutes = " minutes "} else {var plurminutes = " minute "};