如何使用Moment.js获取一个月内的天数列表

eco*_*rvo 25 javascript momentjs

使用Moment.js我希望在一个特定年份的一个月中获得阵列中的所有日子.例如:

January-2014:
[
"01-wed",
"02-thr",
"03-fri",
"04-sat"
]
Run Code Online (Sandbox Code Playgroud)

有什么建议?我查看了Moment.js文档但找不到任何内容.我得到的衣柜是这样的:

moment("2012-02", "YYYY-MM").daysInMonth() 
Run Code Online (Sandbox Code Playgroud)

但是这只返回一个int,其中特定月份的总天数不是每天的数组.

Mar*_*eed 41

这是一个可以解决问题的功能(不使用Moment,而只是使用vanilla JavaScript):

var getDaysArray = function(year, month) {
  var monthIndex = month - 1; # 0..11 instead of 1..12
  var names = [ 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' ];
  var date = new Date(year, monthIndex, 1);
  var result = [];
  while (date.getMonth() == monthIndex) {
    result.push(date.getDate() + "-" + names[date.getDay()]);
    date.setDate(date.getDate() + 1);
  }
  return result;
}
Run Code Online (Sandbox Code Playgroud)

例如:

js> getDaysArray(2012,2)
["1-wed", "2-thu", "3-fri", "4-sat", "5-sun", "6-mon", "7-tue",
 "8-wed", "9-thu", "10-fri", "11-sat", "12-sun", "13-mon", "14-tue",
"15-wed", "16-thu", "17-fri", "18-sat", "19-sun", "20-mon", "21-tue", 
"22-wed", "23-thu", "24-fri", "25-sat", "26-sun", "27-mon", "28-tue",
"29-wed"]
Run Code Online (Sandbox Code Playgroud)

ES2015 +版本:

const getDaysArray = (year, month) => {
  const monthIndex = month - 1
  const names = Object.freeze(
     [ 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' ]);
  const date = new Date(year, monthIndex, 1);
  const result = [];
  while (date.getMonth() == monthIndex) {
    result.push(`${date.getDate()}-${names[date.getDay()]}`);
    date.setDate(date.getDate() + 1);
  }
  return result;
}
Run Code Online (Sandbox Code Playgroud)

请注意,上述解决方案不会在10日之前填充日期,与问题中包含的示例输出不同.使用ES2017 +非常容易修复:

    result.push(`${date.getDate()}`.padStart(2,'0') + `-${names[date.getDay()]}`);
Run Code Online (Sandbox Code Playgroud)

在旧版本的JS中执行它需要滚动自己的零填充逻辑,这并不难,但也不是问题的焦点.


vic*_*chi 11

替代momentjs,为我工作

function getDaysArrayByMonth() {
  var daysInMonth = moment().daysInMonth();
  var arrDays = [];

  while(daysInMonth) {
    var current = moment().date(daysInMonth);
    arrDays.push(current);
    daysInMonth--;
  }

  return arrDays;
}
Run Code Online (Sandbox Code Playgroud)

你可以检查一下

var schedule = getDaysArrayByMonth();
schedule.forEach(function(item) {
  console.log(item.format("DD/MM"));
});
Run Code Online (Sandbox Code Playgroud)


vin*_*nzo 10

使用lodash _.times:

var daysInMonth = [];

var monthDate = moment().startOf('month'); // change to a date in the month of interest

_.times(monthDate.daysInMonth(), function (n) {
     daysInMonth.push(monthDate.format('D'));  // your format
     monthDate.add(1, 'day');
});
Run Code Online (Sandbox Code Playgroud)


tom*_*hes 7

下面是两种不错的功能方法,除了Moment之外,没有任何外部依赖性:

const currentMonthDates = new Array(moment().daysInMonth()).fill(null).map((x, i) => moment().startOf('month').add(i, 'days'));
const currentMonthDates = Array.from({length: moment().daysInMonth()}, (x, i) => moment().startOf('month').add(i, 'days'));
Run Code Online (Sandbox Code Playgroud)

这将返回一个Moment对象数组,然后可以在其上运行所需的任何格式方法。

为了进一步阅读在JavaScript中创建和填充任意长度的数组,还请注意,在第一个示例中,您必须在对数组进行映射之前用null填充数组,因为这样做之前它仍被归类为空,因此map函数不会跑。