使用Date对象查找一个月的所有日子? [英] Find all the days in a month with Date object?

查看:106
本文介绍了使用Date对象查找一个月的所有日子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一种方法可以拥有一个月或一年的所有日子吗?我正在寻找这个,以禁用某个特定日期的日期戳,我会在后台的一个页面选择这些天禁用。



所以我需要在一个月内显示所有的日子,并在每一天下方添加一个激活或禁用按钮。有没有办法找到这些日子与Date对象?我发现这个链接例如:显示一个月的所有日子但是我真的不明白,加上它是Java,我正在尝试在javascript中找到一个解决方案。



感谢您的帮助

解决方案

要获取一个月内所有日子的列表,您可以从 Date 开始一个月的第一天,增加一天直到月份的变化。

  / ** 
* @param {int}月号,0基于
* @param {int}年份,不是零,需要计算闰年
* @return {Date []}列出每月的日期对象
* /
function getDaysInMonth (月,年){
var date = new Date(year,month,1);
var days = [];
while(date.getMonth()=== month){
days.push(new Date(date));
date.setDate(date.getDate()+ 1);
}
退货日
}



示例 http://jsfiddle.net/kKj8h/1/


is there a way to have all the days of a month or of a year? I am looking for this in order to disable some specific days in a datepicker, i would have a page in the back-office to select these days to disable.

So i would need to show all the days in a month, and add a "activate or deactive" button below each day. Is there a way to find these days with the Date object? I found this link for example : Displaying all the days of a month but i don't really understand it, plus it is Java, i am trying to find a solution in javascript.

Thank you for your help

解决方案

To get a list of all days in a month, you can start with a Date on the first day of a month, increase the day until the month changes.

/**
 * @param {int} The month number, 0 based
 * @param {int} The year, not zero based, required to account for leap years
 * @return {Date[]} List with date objects for each day of the month
 */
function getDaysInMonth(month, year) {
     var date = new Date(year, month, 1);
     var days = [];
     while (date.getMonth() === month) {
        days.push(new Date(date));
        date.setDate(date.getDate() + 1);
     }
     return days;
}

Example http://jsfiddle.net/kKj8h/1/

这篇关于使用Date对象查找一个月的所有日子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆