moment.js - 测试日期是今天,昨天,一周还是两周前

时间:2015-06-05 13:55:26

标签: javascript date momentjs

我正在尝试使用moment.js知道日期是今天,昨天,1周前还是更早(2周前或更长时间)。

前两种情况我已经这样做了:

var today = moment().startOf('day');
var yesterday = moment().subtract(1, 'days').startOf('day');

if (moment(localTime).isSame(today, 'd')) // today
    // do something
if (moment(localTime).isSame(yesterday, 'd')) // yesterday
    // do something

这是对的吗?

但是,我如何检查日期是一周前还是更早(例如两周前)?

6 个答案:

答案 0 :(得分:72)

这是有用的东西:

var REFERENCE = moment("2015-06-05"); // fixed just for testing, use moment();
var TODAY = REFERENCE.clone().startOf('day');
var YESTERDAY = REFERENCE.clone().subtract(1, 'days').startOf('day');
var A_WEEK_OLD = REFERENCE.clone().subtract(7, 'days').startOf('day');

function isToday(momentDate) {
    return momentDate.isSame(TODAY, 'd');
}
function isYesterday(momentDate) {
    return momentDate.isSame(YESTERDAY, 'd');
}
function isWithinAWeek(momentDate) {
    return momentDate.isAfter(A_WEEK_OLD);
}
function isTwoWeeksOrMore(momentDate) {
    return !isWithinAWeek(momentDate);
}

console.log("is it today? ..................Should be true: "+isToday(moment("2015-06-05")));
console.log("is it yesterday? ..............Should be true: "+isYesterday(moment("2015-06-04")));
console.log("is it within a week? ..........Should be true: "+isWithinAWeek(moment("2015-06-03")));
console.log("is it within a week? ..........Should be false: "+isWithinAWeek(moment("2015-05-29")));
console.log("is it two weeks older or more? Should be false: "+isTwoWeeksOrMore(moment("2015-05-30")));
console.log("is it two weeks older or more? Should be true: "+isTwoWeeksOrMore(moment("2015-05-29")));

Check a JSFiddle demo包含更多测试,因此如果需要,您可以根据具体情况进行调整。

答案 1 :(得分:15)

更准确的答案如下

var today = moment();
var yesterday = moment().subtract(1, 'day');

var engagementDate = (Date to be compare);

if(moment(engagementDate).isSame(today, 'day'))
  console.log('Today');
else if(moment(engagementDate).isSame(yesterday, 'day'))
  console.log('Yesterday');

答案 2 :(得分:3)

diff功能可能对您的情况有所帮助,一般来说,检查自日期以来的确切天数

var sampleDaysAgo = moment().subtract(28, 'days'); //28 just for test
var today = moment();

console.log(today.diff(sampleDaysAgo, 'days')); // 28 

Demo

答案 3 :(得分:2)

当今最简单的解决方案:

    id       Name       score
0  1234      jim         34,78
1  5678      james       45,80
2  4321      Macy        56

昨天最简单的解决方案:

const result = moment(date).isSame(moment(), "day")

答案 4 :(得分:0)

时刻已经包含了一些这种性质的逻辑

您可能需要查看humanize代码的源代码 https://github.com/moment/moment/blob/ed1fc742b179708d0a987a639979178616309f93/src/lib/duration/humanize.js

fromNow逻辑

https://github.com/moment/moment/blob/497f918515ae6ab900f008f19523b1e4ae5e2450/src/lib/moment/from.js

我发现这很有用,看看他们是如何做到的。

格式化字符串的方式取决于您的业务,说实话,内置逻辑可能在大多数情况下都不会削减它。对于帖子的日期似乎有意义。几天前在哪里'没关系,但是我在邮件中跟踪包裹并说“你的包裹几天前发货了”#39;只是不够好。

答案 5 :(得分:0)

从2.14版开始,您可以自定义calendar()功能

moment().calendar(YOUR_DATE, {
  sameDay: function (now) {
    if (this.isBefore(now)) {
      return '[Will Happen Today]';
    } else {
      return '[Happened Today]';
    }
    /* ... */
  }
});

这是专为“日历”类型的活动而设计的。

http://momentjs.com/docs/#/displaying/calendar-time/