如何使用Javascript AngularJS打印一个月的所有日期
我想显示给定月份的所有日期。假设,如果我选择2015年10月,那么它应该在列表中显示该月的所有日期。我可以使用setDate()打印接下来365天的所有日期,但如何仅打印选定月份的日期。检查此代码以显示接下来的365天如何使用Javascript AngularJS打印一个月的所有日期,javascript,html,angularjs,date,Javascript,Html,Angularjs,Date,我想显示给定月份的所有日期。假设,如果我选择2015年10月,那么它应该在列表中显示该月的所有日期。我可以使用setDate()打印接下来365天的所有日期,但如何仅打印选定月份的日期。检查此代码以显示接下来的365天 函数Ctrl($scope){ $scope.dates=[]; 对于(i=0;i您可以这样尝试: 函数Ctrl($scope){ $scope.dates=[]; var月=3; 风险值年份=2015年; 对于(i=1;i您可以使用 你的代码可能是这样的 function
函数Ctrl($scope){
$scope.dates=[];
对于(i=0;i您可以这样尝试:
函数Ctrl($scope){
$scope.dates=[];
var月=3;
风险值年份=2015年;
对于(i=1;i您可以使用
你的代码可能是这样的
function Ctrl($scope) {
$scope.dates = [];
for (i = 0; i < 365; i++) {
$scope.dates.push((i).days().fromNow());
}
}
函数Ctrl($scope){
$scope.dates=[];
对于(i=0;i<365;i++){
$scope.dates.push((i).days().fromNow());
}
}
如果你能得到一年中一个月的总天数,那么你就可以完成
$scope.month =10; $scope.year =2015; $scope.nDays = new Date( $scope.year, $scope.month, 0).getDate() ;
上述代码的日期为2015年10月日
使用以下代码循环nDays
for (i = 1; i <= $scope.nDays; i++) {
var d = new Date();
$scope.dates.push(d.setDate(d.getDate() + i));}
for(i=1;i你很接近了。正如Jaromanda X所建议的,从1开始,一直到月份变化为止(不要忘记将变量保留在本地)。你每次都需要复制日期:
function Ctrl($scope) {
$scope.dates = [];
var d = new Date(),
i = 1,
m = d.getMonth();
// Set date to start of month
d.setDate(i);
// Store the current month and keep going until it changes
while (d.getMonth() == m) {
// Store dates as a string (format however you wish)
$scope.dates.push('' + d);
// Or store dates as Date objects
$scope.dates.push(new Date(+d));
// Increment date
d.setDate(++i);
}
// return something?
}
编辑
允许输入月份(也是do而非for循环的示例):
或允许输入月份和年份,默认为当前月份和年份:
function Ctrl($scope, month, year) {
$scope.dates = [];
var d = new Date();
d.setFullYear(+year || d.getFullYear(), month - 1 || d.getMonth(), 1);
do {
$scope.dates.push('' + d);
d.setDate(d.getDate() + 1);
} while (d.getDate() != 1)
}
注入即时js库,它将帮助您实现这一目标
这是链接
看一看。您可能会找到您的答案..setDate(1),循环直到getMonth()更改,或者getDate()再次为1,您的选择您可以使用新日期(年、月、0)获取一个月的天数。getDate()
嗨,你能看一下fiddle吗:-它不工作。谢谢。请看这里。它显示10月30天,11月的话还需要额外的12月1日。我不想使用任何第三方库。为什么?这是一个很好的做法,使用其他人以前做的东西。这很好。它显示了当前月份和日期。我f我想在任何时候更改月份?假设,想要获得11月的日期。为月份添加一个参数(请参阅更新的帖子)。您也可以为年份添加一个参数,默认为当前月份和年份。请查看fiddle。您的最后两个代码部分不起作用。您需要更改控制器以接受参数,请参阅。
function Ctrl($scope, month, year) {
$scope.dates = [];
var d = new Date();
d.setFullYear(+year || d.getFullYear(), month - 1 || d.getMonth(), 1);
do {
$scope.dates.push('' + d);
d.setDate(d.getDate() + 1);
} while (d.getDate() != 1)
}