Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/403.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javascript检测每个月的最后一周_Javascript_Datepicker_Date_Week Number_Gettime - Fatal编程技术网

使用javascript检测每个月的最后一周

使用javascript检测每个月的最后一周,javascript,datepicker,date,week-number,gettime,Javascript,Datepicker,Date,Week Number,Gettime,javascript中检测每个(当前)月最后一周的方法是什么。或者月的最后一个星期一?使用getDay()获取月的最后一天的星期几,并从中计算(从月的天数中减去该值可能会起到作用+/-1)。要确定这是否是星期一,请使用.getDay()==1。要确定它是否是该月的最后一天,请添加七天并比较月份:nextMonday.setDate(monday.getDate()+7) nextMonday.getMonth()==monday.getMonth()Javascript“日期”对象是您的朋友 f

javascript中检测每个(当前)月最后一周的方法是什么。或者月的最后一个星期一?

使用
getDay()
获取月的最后一天的星期几,并从中计算(从月的天数中减去该值可能会起到作用+/-1)。

要确定这是否是星期一,请使用
.getDay()==1
。要确定它是否是该月的最后一天,请添加七天并比较月份:
nextMonday.setDate(monday.getDate()+7)
nextMonday.getMonth()==monday.getMonth()

Javascript“日期”对象是您的朋友

function lastOfThisMonth(whichDay) {
  var d= new Date(), month = d.getMonth();
  d.setDate(1); 
  while (d.getDay() !== whichDay) d.setDate(d.getDate() + 1);
  for (var n = 1; true; n++) {
    var nd = new Date(d.getFullYear(), month, d.getDate() + n * 7);
    if (nd.getMonth() !== month)
      return new Date(d.getFullYear(), month, d.getDate() + (n - 1) * 7).getDate();
  }
}
这将为您提供一个月最后一天的日期(以月份为单位,如30),这是一周中选定的一天(0到7)

找到一个月的最后一周取决于你的意思。如果你是指最后一个完整的星期,那么(如果你是指周日-周六)找出最后一个周六,然后减去6。如果您指的是本月开始的最后一周,请查找最后一个星期日。

使用对象及其方法可以执行以下操作

更新

本月最后一个星期一的完整计算可能会压缩到

var d = new Date();
d.setMonth( d.getMonth() + 1 );
d.setDate(0);
lastmonday = d.getDate() - (d.getDay() - 1);
alert(lastmonday);

冗长的例子

var now = new Date(); // get the current date

// calculate the last day of the month
if (now.getMonth() == 11 )  // if month is dec then go to next year and first month
{
    nextmonth = 0;
    nextyear = now.getFullYear() + 1;
}
else // otherwise go to next month of current year
{
  nextmonth = now.getMonth() + 1;
  nextyear = now.getFullYear();
}

var d = new Date( nextyear , nextmonth , 0); // setting day to 0 goes to last date of previous month
alert( d.getDay() ); // will alert the day of the week 0 being sunday .. you can calculate from there to get the first day of that week ..

我建议获取月份的天数,然后从最后一天开始循环,直到getDay()返回星期一(1)或星期天(0)。。根据你的一周什么时候开始。一旦你确定了开始日期。。。结束日期应该是startDate+7,所以沿着这些线

我发现:

//Create a function that determines how many days in a month
//NOTE: iMonth is zero-based .. Jan is 0, Feb is 2 and so on ...
function daysInMonth(iMonth, iYear)
{
    return 32 - new Date(iYear, iMonth, 32).getDate();
}
然后循环:

//May - should return 31
var days_in_month = daysInMonth(4, 2010);

var weekStartDate = null;
var weekEndDate = null;    

for(var i=days_in_month; i>0; i--)
{
  var tmpDate = new Date(2010, 4, i);
  //week starting on sunday
  if(tmpDate.getDay() == 0)
  {
    weekStartDate = new Date(tmpDate);
    weekEndDate = new Date(tmpDate.setDate(tmpDate.getDate() + 6));
    //break out of the loop
    break; 
  }
}

您还可以在给定日期之前或之后找到第三个星期一或第一个星期二, 或者在两个日期之间的每周三挂国旗

Date.prototype.lastweek= function(wd, n){
    n= n || 1;
    return this.nextweek(wd, -n);
}
Date.prototype.nextweek= function(wd, n){
    if(n== undefined) n= 1;
    var incr= (n<0)? 1: -1,
    D= new Date(this),
    dd= D.getDay();
    if(wd=== undefined) wd= dd;
    if(dd!= wd) while(D.getDay()!= wd) D.setDate(D.getDate()+incr);
    D.setDate(D.getDate()+7*n);
    D.setHours(0, 0, 0, 0);
    return D;
}


function lastMondayinmonth(month, year){
    var day= new Date();
    if(!month) month= day.getMonth()+1;
    if(!year) year= day.getFullYear();
    day.setFullYear(year, month, 0);
    return day.lastweek(1);
}
alert(lastMondayinmonth())
Date.prototype.lastweek=函数(wd,n){
n=n | | 1;
返回此.nextweek(wd,-n);
}
Date.prototype.nextweek=函数(wd,n){
如果(n==未定义)n=1;

var incr=(n我发现这样的示例可以检测每周的最后一个星期一,但它不会检测每月的最后一个星期一。也许这有助于找到更好的解决方案,因为代码看起来很短

var dif, d = new Date(); // Today's date
dif = (d.getDay() + 6) % 7; // Number of days to subtract
d = new Date(d - dif * 24*60*60*1000); // Do the subtraction

alert(d); // Last monday.

获取当月的最后一天:

/**
 * Accepts either zero, one, or two parameters.
 *     If zero parameters: defaults to today's date
 *     If one parameter: Date object
 *     If two parameters: year, (zero-based) month
 */
function getLastDay() {
    var year, month;
    var lastDay = new Date();

    if (arguments.length == 1) {
        lastDay = arguments[0];
    } else if (arguments.length > 0) {
        lastDay.setYear(arguments[0]);
        lastDay.setMonth(arguments[1]);
    }

    lastDay.setMonth(lastDay.getMonth() + 1);
    lastDay.setDate(0);

    return lastDay;
}
获取最后一个星期一:

/**
 * Accepts same parameters as getLastDay()
 */
function getLastMonday() {
    var lastMonday = getLastDay.apply(this, arguments);
    lastMonday.setDate(lastMonday.getDate() - (lastMonday.getDay() == 0 ? 6 : (lastMonday.getDay() - 1)));
    return lastMonday;
}
获取给定日期的年度工作周:

/**
 * Accepts one parameter: Date object.
 * Assumes start of week is Sunday.
 */
function getWeek(d) {
    var jan1 = new Date(d.getFullYear(), 0, 1);
    return Math.ceil((((d - jan1) / (24 * 60 * 60 * 1000)) + jan1.getDay() + 1) / 7);
}
将它们放在一起(假设您使用的是Firebug):


好的,到目前为止,我提出了这样的解决方案,让它成为我自己的方式,并在这里提到了一些事情。它工作正常,总是返回本月的最后一个星期一

//function that will help to check how many days in month
function daysInMonth(iMonth, iYear)
{
    return 32 - new Date(iYear, iMonth, 32).getDate();
}


var dif = null;
d = new Date(); // Today's date
countDays = daysInMonth(d.getMonth(),d.getFullYear()); //Checking number of days in current month
d.setDate(countDays); //setting the date to last day of the month
dif = (d.getDay() + 6) % 7; // Number of days to subtract
d = new Date(d - dif * 24*60*60*1000); // Do the subtraction
alert(d.getDate()); //finally you get the last monday of the current month

+一个好问题,我即将写一个自定义日期选择器,并开始问自己类似的问题。这是本月的最后一周还是本月开始的最后一周?你的周是从周日还是周一开始?他确实提到了本月的最后一个周一。显然,没有必要循环查找最后一个日期;sim卡ple部门会想出来的,但我太懒/笨了,不知道它到底是什么样子。我真的不喜欢在几天内循环,希望我们能避免它。好吧,至少它一次跳过7次:)让我想想。看起来是一个有效且非常简短的解决方案,确实是上周一返回的,还会进行更多测试,其他人也会这么想吗?好的,我发现该代码有问题。出于某种原因,如果一个月的最后一天实际上是星期一,那么它将在下个月的最后一个星期一发出警报。@Alex,本月(2010年5月)在一个月的最后一天(2010年5月31日)有一个星期一。如果运行alerts 31,那么代码…你能给出另一个例子(你发现是错误的)吗,这样我就可以测试它并找出问题?
//function that will help to check how many days in month
function daysInMonth(iMonth, iYear)
{
    return 32 - new Date(iYear, iMonth, 32).getDate();
}


var dif = null;
d = new Date(); // Today's date
countDays = daysInMonth(d.getMonth(),d.getFullYear()); //Checking number of days in current month
d.setDate(countDays); //setting the date to last day of the month
dif = (d.getDay() + 6) % 7; // Number of days to subtract
d = new Date(d - dif * 24*60*60*1000); // Do the subtraction
alert(d.getDate()); //finally you get the last monday of the current month