我想创建这样的数组。
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>
由于
答案 0 :(得分:3)
您需要Do
令牌,请参阅Display Format
moment(start).format('ddd - Do MMM')
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;