【问题标题】:Javascript time, pluralities 1 min vs 1 mins. Timer for time notification receivedJavascript 时间,复数 1 分钟对 1 分钟。收到时间通知的计时器
【发布时间】:2017-02-27 17:06:39
【问题描述】:
self.calcMessageAge = function calcMessageAge(fireDate){
    var date = Date.parse(fireDate);
    var now = new Date();
    now = Date.now();

    // in seconds
    var diff = Math.abs(date - now)/1000;
    if(diff < 60) {
        return "1 min";
    }else if(diff < (60*60) ) {
        // minutes
        return Math.floor(diff/60) + " mins";

    } else if(diff < (60*60*24)) {
        // hours
        var hrs = Math.floor(diff/60/60) + " hours";
        if(hrs !== "1 hours") {
            return Math.floor(diff/60/60) + " hours";
        } else {
            return Math.floor(diff/60/60) + " hour";
        }
    } else if(diff < (60*60*24*7)) {
        // days
        var _day = Math.floor(diff/24/60/60) + " days";
        if(_day !== "1 days") {
            return Math.floor(diff/24/60/60) + " days";
        } else {
            return Math.floor(diff/24/60/60) + " day";
        }

    } else if(diff < (60*60*24*7*4)) {
        // weeks
        var _weeks = Math.floor(diff/7/24/60/60) + " weeks";
        if(_weeks !== "1 weeks") {
            return Math.floor(diff/7/24/60/60) + " weeks";
        } else {
            return Math.floor(diff/7/24/60/60) + " week";
        }
    } else {
        var _months = Math.floor(diff/4/7/24/60/60) + " months";
        if(_months !== "1 months") {
            return Math.floor(diff/4/7/24/60/60) + " months";
        } else {
            return Math.floor(diff/4/7/24/60/60) + " month";
        }
    }
};

上述方法应根据通知到达 UI 的时间 (fireDate) 返回一个字符串。 字符串,例如“1 分钟前”或“1 天前”,然后是“2 分钟前”或“2 天前”

我正确收到“1 分钟前”,但在 60 秒时我收到“1 分钟”返回,这也发生在天数。

我认为这与我检查 60 秒时应该是 59 的事实有关吗?我真的有点难过。任何帮助将不胜感激。

【问题讨论】:

  • 使用&lt;=而不是&lt;进行比较
  • 我……什么?您进行数学运算并连接一个字符串,然后根据结果再次进行数学运算并连接相同的字符串或不同的字符串?做一次数学运算,检查结果,然后连接适当的字符串不是更有意义吗?如果没有别的,它会使它更容易阅读。
  • @DaveNewton 你是绝对正确的。在这一点上,我的任务是对此进行快速而肮脏的修复。我继承了代码,这是我第一次看到这是如何完成的。相信我,我会在一两个星期内做对。当我有一点时间的时候。
  • 标题似乎是一堆单词拼凑而成的。你能edit那更能描述你的问题吗?
  • diff &lt; 60 应该只是 diff &lt;= 60。如果你想获得技术,从技术上讲,它在 2 分钟之前是 1 min ago,所以你可以这样做 diff &lt; 120

标签: javascript angularjs ajax date


【解决方案1】:

你可以这样做

const getDiffrence = fireDate => {
  const date = new Date(fireDate).getTime();
  let now = new Date().getTime();
  const diff = Math.abs(date - now) / 1000
  const min = Math.floor(diff/60) % 60 || ''
  const h = Math.floor(diff/60/60) % 60 || ''
  const day = Math.floor(diff/60/60/24) % 24 || ''
  const week = Math.floor(diff/60/60/24/7)|| ''
  const month = Math.floor(diff/4/7/24/60/60) || ''
  return `${month ? month + 'months' : ''} ${week ? week + 'weeks' : ''} ${day ? day + 'days' : ''} ${day ? day + 'days' : ''} ${h ? h + 'hours' : ''} ${min ? min + 'mins' : ''} `.replace(/\b(1\D+?)s/g, '$1').trim()
}

console.log(getDiffrence('2017/02/27 18:22'))
console.log(getDiffrence(new Date().getTime() - 60 * 1000 - 5000))

这段代码的主要部分是.replace(/\b(1\D+?)s/g, '$1') 这将在.replace(/(0\D+?)s\s*/g, '') 之后将所有1 anythings 替换为1 anything 或evnen crazer,你只需要创建像'1days 0hours 0minutes' 这样的字符串

console.log('11mounths 1days 0hours 20minutes'.replace(/\b0\D+?s\s*/g, '').replace(/\b(1\D+?)s/g, '$1'))

【讨论】:

    【解决方案2】:

    var calcMessageAge = function(fireDate) {
      var date = Date.parse(fireDate);
      var now = Date.now();
      // in seconds
      var diff = Math.abs(date - now) / 1000;
      var minute = 60;
      var hour = 60 * minute;
      var day = 24 * hour;
      var week = 7 * day;
      var month = 4 * week; // month is usually longer than 4 weeks but I let the same as in original code
      var num;
      var label;
    
      if (diff > month) {
        num = Math.floor(diff / month);
        label = num + ' month' + (num > 1 ? 's': '');
      } else if (diff > week) {
        num = Math.floor(diff / week);
        label = num + ' week' + (num > 1 ? 's': '');
      } else if (diff > day) {
        num = Math.floor(diff / day);
        label = num + ' day' + (num > 1 ? 's': '');
      } else if (diff > hour) {
        num = Math.floor(diff / hour);
        label = num + ' hour' + (num > 1 ? 's': '');
      } else if (diff > minute) {
        num = Math.floor(diff / minute);
        label = num + ' min' + (num > 1 ? 's': '');
      } else {
        label = '1 min';
      }
    
      return label;
    };
    
    console.log(calcMessageAge('02-02-2017'));
    console.log(calcMessageAge('01-01-2017 20:00'));
    console.log(calcMessageAge(new Date(new Date().getTime() - 60 * 1000)));
    console.log(calcMessageAge(new Date(new Date().getTime() - 59 * 1000)));
    console.log(calcMessageAge(new Date(new Date().getTime() - 60 * 60 * 4 * 1000)));

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 2017-12-22
      • 2021-03-07
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-03
      • 2012-07-23
      相关资源
      最近更新 更多