如何使用Javascript检查/计算周日计数(使用Date函数)? [英] How to check/calculate the week-day count (using Date functions) using Javascript?

查看:110
本文介绍了如何使用Javascript检查/计算周日计数(使用Date函数)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个给定的日期是在日期的第一个,第二个,第三个,第四个还是最后一个星期一,星期二,星期三,...星期日。

I would like to check if a given date is the first, second, third, fourth or last monday, tuesday, wednesday,..., sunday in the dates month.

该函数应如下所示:

if(checkdate(date, "second", "monday"))
{
   alert("This is the second monday of the month");
}
else
{
   alert("This is NOT the second monday of the month");
}


推荐答案

我宁愿写一个函数它返回一个对象,告诉我给定日期的日期和周数。如下:

I would rather write a function that returns an object telling me the day and the week-number-in-month of the given date. Something like:

function infoDate(date) {
 return {
  day: date.getDay()+1,
  week: (date.getDate()-1)%7+1
 }
}

然后我可以读取返回的对象,找出是否是第二个星期一或者什么:

Then I can read the returned object and find out if it's the second monday or whatever:

var info = infoDate(date);
if(info.day==1 && info.week==2) {
  // second monday
}

当然,您仍然可以根据数字和日期名称数组来编写另一个本地化函数,完全符合您的要求。

Of course, you can still write another localized function that does exactly what you ask for based on an array of numeral and day names.

这篇关于如何使用Javascript检查/计算周日计数(使用Date函数)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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