使用 Date 对象查找一个月中的所有天数? [英] Find all the days in a month with Date object?
问题描述
有没有办法获得一个月或一年的所有日子?我正在寻找这个以禁用日期选择器中的某些特定日期,我会在后台有一个页面来选择这些日期以禁用.
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.
所以我需要显示一个月中的所有天数,并在每一天下方添加一个激活或停用"按钮.有没有办法用 Date 对象找到这些日子?我找到了这个链接,例如:显示一个月的所有日子但我真的不明白,加上它是 Java,我正在尝试在 javascript 中找到解决方案.
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.
感谢您的帮助
推荐答案
要获取一个月中所有天的列表,可以从一个月的第一天的 Date
开始,增加直到月份发生变化的那一天.
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;
}
UTC 版本
为了回应一些评论,我创建了一个使用 UTC 方法的版本,以防您想调用 UTC 方法而不是返回本地化时区的标准方法.
UTC Version
In response to some comments, I've created a version that uses UTC methods in case you want to call UTC methods instead of the standard methods that return the localized time zone.
我怀疑这是评论说这不起作用的罪魁祸首.如果您使用 Date.UTC
实例化它,您通常希望确保调用 getUTCMonth/Day/Hours
方法,反之亦然,除非您尝试转换时区并显示差异.
I suspect this is the culprit of the comments saying this didn't work. You typically want to make sure you call getUTCMonth/Day/Hours
methods if you instantiated it with Date.UTC
, and vice-versa, unless you are trying to convert time zones and show differences.
function getDaysInMonthUTC(month, year) {
var date = new Date(Date.UTC(year, month, 1));
var days = [];
while (date.getUTCMonth() === month) {
days.push(new Date(date));
date.setUTCDate(date.getUTCDate() + 1);
}
return days;
}
编辑此答案
如果您认为此脚本有问题,请随时:
Editing This Answer
If you think there's a problem with this script, please feel free to:
- 首先查看下面的现有单元测试
- 编写一个测试用例来证明它已损坏.
- 修复代码,确保现有测试通过.
/**
* @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 getDaysInMonthUTC(month, year) {
var date = new Date(Date.UTC(year, month, 1));
var days = [];
while (date.getUTCMonth() === month) {
days.push(new Date(date));
date.setUTCDate(date.getUTCDate() + 1);
}
return days;
}
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;
}
const days2020 = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
const days2021 = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
describe("getDaysInMonthUTC", function() {
it("gets day counts for leap years", function() {
const actual = days2020.map(
(day, index) => getDaysInMonthUTC(index, 2020).length
);
expect(actual).toEqual(days2020);
});
it("gets day counts for non-leap years", function() {
const actual = days2021.map(
(day, index) => getDaysInMonthUTC(index, 2021).length
);
expect(actual).toEqual(days2021);
});
});
describe("getDaysInMonth", function() {
it("gets day counts for leap years", function() {
const actual = days2020.map(
(day, index) => getDaysInMonth(index, 2020).length
);
expect(actual).toEqual(days2020);
});
it("gets day counts for non-leap years", function() {
const actual = days2021.map(
(day, index) => getDaysInMonth(index, 2021).length
);
expect(actual).toEqual(days2021);
});
});
// load jasmine htmlReporter
(function() {
var env = jasmine.getEnv();
env.addReporter(new jasmine.HtmlReporter());
env.execute();
}());
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.js"></script>
<script src="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine-html.js"></script>
<link href="https://cdn.jsdelivr.net/jasmine/1.3.1/jasmine.css" rel="stylesheet"/>
这篇关于使用 Date 对象查找一个月中的所有天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!