以下代码来自moment.js文档
moment().date(Number);
moment().date(); // Number
moment().dates(Number);
moment().dates(); // Number
但输入parameter
数据类型是数字而不是短月名称和年份,这是我的要求输入。
下面是我的输入格式,如数组对象
`$scope.allMonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Act", "Nov", "Dec"];
$scope.Year=2017;// `
所以我有月份和年份,现在我希望通过使用moment.js来获取所有日期和日期
我在Answer下面使用普通的javascript得到了确切的结果。但仍然无法通过使用moment.js
找到解决方案
答案 0 :(得分:3)
function getMonths(month,year){
var ar = [];
var start = moment(year+"-"+month,"YYYY-MMM");
for(var end = moment(start).add(1,'month'); start.isBefore(end); start.add(1,'day')){
ar.push(start.format('D-ddd'));
}
return ar;
}
console.log(getMonths('Mar',2011))
答案 1 :(得分:0)
最后我通过javascript获得了它
//selected year
$scope.selectedYear = function (value) {
$scope.selectedYearValue = value;// 2011
}
//get days and date from a month and year
$scope.getDaysArray = function (month) {// month count is 2
var names = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
var date = new Date($scope.selectedYearValue, month - 1, 1);
$scope.DayAndDate = [];
while (date.getMonth() == month - 1) {
result.push({ "Date": date.getDate(), "Day": names[date.getDay()] });
$scope.DayAndDate.setDate(date.getDate() + 1);
}
}
现在结果是
js> getDaysArray(2012)
["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"]
但我不想使用JavaScript代替moment.js 。