按moment.js创建日历数组

时间:2017-06-06 12:43:34

标签: javascript jquery arrays json momentjs

我想创建这样的数组。

var date_ = [
{ "DateText": "Mon - 2nd April", "Date": "2/4/2017", "WeekNumber" : 13 },
{ "DateText": "Tue - 3rd April", "Date": "3/4/2017", "WeekNumber": 14 },
{ "DateText": "Wed - 4th April", "Date": "4/4/2017", "WeekNumber": 14 }
];

我得到的只是第2,第3和第4号数字约定,你可以查看我的代码。

var date_ = []; 

var prevDate = moment().subtract(15, 'days');
var nextDate = moment().add(15, 'days');

var start = new Date(prevDate);
var end = new Date(nextDate);

while(start < end){
   date_.push(
      {
        "DateText" : moment(start).format('ddd - DD MMM'),
        "Date" : moment(start).format('DD/MM/YYYY'),
        "WeekNumber" : moment(start).week()
      }
    );
   var newDate = start.setDate(start.getDate() + 1);
   start = new Date(newDate);  
}

console.log(date_);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>

由于

1 个答案:

答案 0 :(得分:3)

您需要Do令牌,请参阅Display Format

moment(start).format('ddd - Do MMM')

&#13;
&#13;
var date_ = [];

var prevDate = moment().subtract(15, 'days');
var nextDate = moment().add(15, 'days');

while (prevDate.isBefore(nextDate)) {
  date_.push({
    "DateText": prevDate.format('ddd - Do MMM'),
    "Date": prevDate.format('DD/MM/YYYY'),
    "WeekNumber": prevDate.week()
  });
  prevDate.add(1, 'days');
}

console.log(date_);
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.13.0/moment.min.js"></script>
&#13;
&#13;
&#13;