JavaScript:获取给定日期(月/年)的日期名称数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11322281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaScript: get array of day names of given date (month/year)
提问by Chris
how can i get the whole month in day namesof given month/year? like :
我怎样才能在给定的月份/年份的日期名称中获得整个月份?喜欢 :
var year = "2000";
var month = "7"
... some code here that makes an array with names of days of the given month...
and the output looks something like:
输出类似于:
Array ("1. Sunday", "2. Monday", "3. Tuesday", ... "29. Thursday", "30. Friday", "31. Saturday");
Best regards, Chris
最好的问候,克里斯
回答by natlee75
This should do what you asked.
这应该按照你的要求做。
function getDaysArray(year, month) {
var numDaysInMonth, daysInWeek, daysIndex, index, i, l, daysArray;
numDaysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
daysInWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
daysIndex = { 'Sun': 0, 'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu': 4, 'Fri': 5, 'Sat': 6 };
index = daysIndex[(new Date(year, month - 1, 1)).toString().split(' ')[0]];
daysArray = [];
for (i = 0, l = numDaysInMonth[month - 1]; i < l; i++) {
daysArray.push((i + 1) + '. ' + daysInWeek[index++]);
if (index == 7) index = 0;
}
return daysArray;
}
回答by Chris
Try this
试试这个
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to display todays day of the week.</p>
<button onclick="myFunction()">Try it</button>
<script type="text/javascript">
function daysInMonth(month,year) {
return new Date(year, month, 0).getDate();
}
function myFunction()
{var b=[],weekday = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],month=7,year=2000;
for(var i=1,l=daysInMonth(month,year);i<l;i++){
var d = new Date(year,month-1,i);
b.push(i+"."+weekday[d.getDay()]);
}
console.log(b);}//b is the desired array
</script>
</body>
</html>
回答by scottheckel
You can do the logic yourself pretty easily using the standard JavaScript Date object. Here's an example of getting one date to push into your array. Just loop through the entire month calling the method. (jsFiddle)
您可以使用标准的 JavaScript Date 对象很容易地自己完成逻辑。这是获取一个日期以推送到您的数组的示例。只需循环调用该方法即可。( jsFiddle)
var daysOfTheWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
function getDisplay(month, day, year) {
var d = new Date(year, month-1, day);
return day + '. ' + daysOfTheWeek[d.getDay()];
}
array_push($dates, getDisplay(7, 4, 2012));
回答by diEcho
Try this and see the result in console.
试试这个并在控制台中查看结果。
<script>
var weekday = ['Sunday', 'Monday', 'Tuesday', 'Wednesday',
'Thursday', 'Friday', 'Saturday'];
var month = '7';
var year = '2012';
realMonth = parseInt(month,10)-1;
var monthObj = {};
for(var i=1;i<30;i++)
{
var d = new Date(year,realMonth,i);
monthObj[i] = weekday[d.getDay()];
}
console.log(monthObj);
</script>
回答by Arithmomaniac
回答by apple apple
I know this is an old question, but today a questionlinked to this, and I'd like to point out one don't really need to do much things other than convert day (0-6)
to string (Sunday-Saturday)
我知道这是一个老问题,但今天有一个与此相关的问题,我想指出除了转换day (0-6)
为string (Sunday-Saturday)
function getDays(year, month) { //month: 1-based, as normal person
--month //change it to 0-based, as Date object
let dayname = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
let date = new Date(year, month)
let result = []
while(date.getMonth()==month){
result.push(`${date.getDate()}. ${dayname[date.getDay()]}`)
date.setDate(date.getDate()+1)
}
return result;
}
console.log(getDays(2019,12))