Moment JS确定多少天,几周过去了

时间:2018-07-20 08:03:34

标签: javascript datetime momentjs datetime-format relative-time-span

我不太擅长处理日期时间。我使用了momentjs ionic应用来操纵时间,但我想实现我无法实现的目标。

我为此使用了一个管道,我想根据是否经过了几天或几周,几个月或几年来显示。使用相对时间可以帮助我,就像{strong> momentjs 的fromNow()方法和calendar()。但就我而言,我会有多个conditions

这是我的管道下面的示例代码

transform(value: Date | moment.Moment, dateFormat: string): any {
    if (moment(value) < moment(value).subtract(7, 'days')) {
      return moment(value).format('llll') // Use this format if weeks, months or years has passed
    } else if (moment(value) < moment(value).subtract(1, 'days')) {
      return moment(value).calendar(); // Use calendar time if 1 day has passed
    } else {
      return moment(value).fromNow(); // Use relative time if within 24 hours
    }
  }

如果直到24小时过去了几秒钟,几分钟或几小时,我将使用fromNow()方法,但是如果经过几天,我将使用calendar(),如果经过了几周,几个月或几年,则使用此{{1 }}。

有人可以给我一些启发吗?

谢谢。

2 个答案:

答案 0 :(得分:3)

据我了解,您想根据某个特定时间段now的发生时间来做出决定。您似乎有3种情况:> 7天,> 1天,<1天。

Momentjs提供了一个非常有用的diff方法。因此,您可以执行以下操作:

  var currDate = moment.now();
  var dateToTest = moment(val);
  // if dateToTest will always be in past, use currDate as the base to diff, else 
  be prepared to handle the negative outcomes. 
  var result = currDate.diff(dateToTest, 'days')

window.onload = function() {
  console.log("Test Cases: ")
  console.log("Input: Date is 2 minutes behind")
  dateThing("2018-07-20T12:02:54+00:00");
  
  console.log("Input: Date is few hours behind")
  dateThing("2018-07-20T07:02:54+00:00");
  
  console.log("Input: Date is 23 hours 59 minutes behind")
  dateThing("2018-07-19T12:03:54+00:00");
  
  console.log("Input: Date is 24 hours behind")
  dateThing("2018-07-19T12:04:54+00:00");
  
  console.log("Input: Date is 2 days behind")
  dateThing("2018-07-18T12:04:54+00:00");
  
  console.log("Input: Date is 12 days behind")
  dateThing("2018-07-08T12:04:54+00:00");
}

dateThing = function(val) {
  // for now freezing the "now" so that precise testcases can be written.
  // var currDate = moment.now();
  var currDate = moment("2018-07-20T12:04:54+00:00")
  var dateToTest = moment(val);
  // if dateToTest will always be in past, use currDate as the base to diff, else be prepared to handle the negative outcomes. 
  var result = currDate.diff(dateToTest, 'days')
  if (result > 7) {
    console.log("Output: date is more than 1 week behind")
  } else if (result > 1) {
    console.log("Output: date is more than 1 day but less than 1 week behind")
  } else {
    console.log("Output: date is less  than 1 day behind")
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>

请运行上面的代码片段以查看边界案例的行为,如果不正确,您可以花几分钟进行比较,然后逆向流程。

答案 1 :(得分:0)

你去了。 :)

function transformDate(givenDate) {
    if (moment().subtract(7, 'days').valueOf() > moment(givenDate).valueOf()) {
      return moment(givenDate).format('llll');
    } else if (moment().subtract(1, 'days').valueOf() > moment(givenDate).valueOf()) {
      return moment(givenDate).calendar();
    } else {
      return moment(givenDate).fromNow();
    }
}
// current time
console.log(transformDate(new Date()));
// 8 days before this answer was posted 
console.log(transformDate(new Date(1531394211385))); 
// 2 days before this answer was posted
console.log(transformDate(new Date(1531912692803))); 
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>