用momentjs计算公共日期范围

时间:2018-02-02 11:11:20

标签: momentjs

我想用moment.js计算常见日期范围,例如: *昨天 *周 *本周至今 * 25美分硬币 *季度至今

等。我没有在Stackoverflow上找到我想要的东西,所以我想如果它有帮助我会发布给别人的。

1 个答案:

答案 0 :(得分:1)

  // Yesterday
  console.log("yesterday", moment().startOf('day').subtract(1, 'day').format('MM-DD-YYYY'), moment().startOf('day').subtract(1, 'day').format('MM-DD-YYYY'));
  console.log("");

  // Week
  console.log("week", moment().startOf('week').format('MM-DD-YYYY'), moment().endOf('week').format('MM-DD-YYYY'));
  console.log("week-to-date", moment().startOf('week').format('MM-DD-YYYY'), moment().format('MM-DD-YYYY'));
  console.log("prior-week", moment().startOf('week').subtract(1, 'week').format('MM-DD-YYYY'), moment().endOf('week').subtract(1, 'week').endOf('week').format('MM-DD-YYYY'));
  console.log("prior-week-to-date", moment().startOf('week').subtract(1, 'week').format('MM-DD-YYYY'), moment().subtract(1, 'week').format('MM-DD-YYYY'));
  console.log("prior-year-week", moment().startOf('week').subtract(1, 'year').format('MM-DD-YYYY'), moment().endOf('week').subtract(1, 'year').format('MM-DD-YYYY'));
  console.log("prior-year-week-to-date", moment().startOf('week').subtract(1, 'year').format('MM-DD-YYYY'), moment().subtract(1, 'year').format('MM-DD-YYYY'));
  console.log("");

  // Months
  console.log("month", moment().startOf('month').format('MM-DD-YYYY'), moment().endOf('month').format('MM-DD-YYYY'));
  console.log("month-to-date", moment().startOf('month').format('MM-DD-YYYY'), moment().format('MM-DD-YYYY'));
  console.log("prior-month", moment().startOf('month').subtract(1, 'month').format('MM-DD-YYYY'), moment().endOf('month').subtract(1, 'month').endOf('month').format('MM-DD-YYYY'));
  console.log("prior-month-to-date", moment().startOf('month').subtract(1, 'month').format('MM-DD-YYYY'), moment().subtract(1, 'month').format('MM-DD-YYYY'));
  console.log("prior-year-month", moment().startOf('month').subtract(1, 'year').format('MM-DD-YYYY'), moment().endOf('month').subtract(1, 'year').format('MM-DD-YYYY'));
  console.log("prior-year-month-to-date", moment().startOf('month').subtract(1, 'year').format('MM-DD-YYYY'), moment().subtract(1, 'year').format('MM-DD-YYYY'));
  console.log("");

  // Quarters
  console.log("quarter", moment().startOf('quarter').format('MM-DD-YYYY'), moment().endOf('quarter').format('MM-DD-YYYY'));
  console.log("quarter-to-date", moment().startOf('quarter').format('MM-DD-YYYY'), moment().format('MM-DD-YYYY'));
  console.log("prior-quarter", moment().startOf('quarter').subtract(1, 'quarter').format('MM-DD-YYYY'), moment().endOf('quarter').subtract(1, 'quarter').endOf('quarter').format('MM-DD-YYYY'));
  console.log("prior-quarter-to-date", moment().startOf('quarter').subtract(1, 'quarter').format('MM-DD-YYYY'), moment().subtract(1, 'quarter').format('MM-DD-YYYY'));
  console.log("prior-year-quarter", moment().startOf('quarter').subtract(1, 'year').format('MM-DD-YYYY'), moment().endOf('quarter').subtract(1, 'year').endOf('quarter').format('MM-DD-YYYY'));
  console.log("prior-year-quarter-to-date", moment().startOf('quarter').subtract(1, 'year').format('MM-DD-YYYY'), moment().subtract(1, 'year').format('MM-DD-YYYY'));
  console.log("");

  // years
  console.log("year", moment().startOf('year').format('MM-DD-YYYY'), moment().endOf('year').format('MM-DD-YYYY'));
  console.log("year-to-date", moment().startOf('year').format('MM-DD-YYYY'), moment().format('MM-DD-YYYY'));
  console.log("prior-year", moment().startOf('year').subtract(1, 'year').format('MM-DD-YYYY'), moment().endOf('year').subtract(1, 'year').endOf('year').format('MM-DD-YYYY'));
  console.log("prior-year-to-date", moment().startOf('year').subtract(1, 'year').format('MM-DD-YYYY'), moment().subtract(1, 'year').format('MM-DD-YYYY'));
  console.log("");