在JS / Prototype中获取一个月的几天 [英] get days of a month in JS/Prototype
本文介绍了在JS / Prototype中获取一个月的几天的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种方法,它返回给定月份的所有日子。在标准的 date()函数中找不到任何帮助。
I´m looking for a method, which returns all days of a given month. I couldn't find anything helpful at the standard date() functions.
ie:
getDays(2010-05) // returns something like:
{ day:'Saturday', date:'2010-01-05' },
{ day:'Sunday', date:'2010-02-05' },
{ day:'Monday', date:'2010-03-05' }[...]
Just the day of the first day of the month may (saturday, 01) and the last (monday, 31) would be great, too.
推荐答案
你可以很容易地做到这一点。 JavaScriptDate对象对于这样的事情是非常有帮助的:
You can do this yourself pretty easily. The Javascript "Date" object is a great help for things like this:
function listDays(monthNumber) {
var days = [],
today = new Date(),
d = new Date(today.getFullYear(), monthNumber, 1);
while (d.getMonth() == monthNumber) {
days.push({ "date": d.getDate(), "weekday": d.getDay()});
d.setDate(d.getDate() + 1);
}
return days;
}
只能在数字上返回月份的日子,但很容易使用一系列日期名称来扩展:
That only returns the days in the month numerically, but it'd be easy to extend with an array of day-of-week names:
function listDays(monthNumber) {
var days = [],
today = new Date(),
d = new Date(today.getFullYear(), monthNumber, 1),
weekdays = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
while (d.getMonth() == monthNumber) {
days.push({ "date": d.getDate(), "weekday": weekdays[d.getDay()]});
d.setDate(d.getDate() + 1);
}
return days;
}
这篇关于在JS / Prototype中获取一个月的几天的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文