簡體   English   中英

是否有一種簡單的方法來查找當前月份中一周中某天的最后日期

[英]Is there an easy way to find the last date a day of week occurs in the current month

我試圖顯示當前月份的最后一個星期三的日期,以便在下個月出現時它將自動更改為正確的日期。 (因此,不必說:“執行每個月的最后一個星期三”,我可以動態地給出實際日期。)

例如,我希望日期在該月的9月25日星期三顯示在網頁上,然后在下個月的10月30日星期三顯示。

如果我可以在下一個日期過去之后再顯示下個月的日期,那將是一個額外的解決方案。 在我上面的示例中,當當前日期為9月26日至30日(該日期為該最后一個星期三之后的任何日期,但仍在同一月)。該日期將顯示下一個執行日期10月30日。

如果解決方案是通過html,javascript / jquery或asp來解決,那就太好了。

謝謝,SunnyOz

這取決於您“輕松”的標准。 這是一個根據需要執行的簡單函數,它的5行工作代碼可以減少為4行,但是如果這樣做,將會失去一些清晰度:

function lastDayInMonth(dayName, month, year) {
  // Day index map - modify to suit whatever you want to pass to the function
  var dayNums = {Sunday: 0, Monday:1, Tuesday:2, Wednesday:3, 
                 Thursday:4, Friday:5, Saturday:6};

  // Create a date object for last day of month
  var d = new Date(year, month, 0);

  // Get day index, make Sunday 7 (could be combined with following line)
  var day = d.getDay() || 7;

  // Adjust to required day
  d.setDate(d.getDate() - (7 - dayNums[dayName] + day) % 7);

  return d;
}

您可以將地圖更改為任何內容,只需確定要傳遞給可映射到ECMAScript天數的函數(日期名稱,縮寫,索引等)即可。

編輯

因此,如果一直希望顯示該月或下個月的最后一個星期三(如果已通過):

function showLastWed() {
  var now = new Date();
  var lastWedOfThisMonth = lastDayInMonth('Wednesday', now.getMonth()+1, now.getFullYear());
  if (now.getDate() > lastWedOfThisMonth().getDate()) {
    return lastDayInMonth('Wednesday', now.getMonth()+2, now.getFullYear());
  } else {
    return lastWedOfThisMonth;
  }
}

請注意,該函數期望日歷月份數(Jan = 1,Feb = 2,等等),而getMonth方法返回ECMAScript月索引(Jan = 0,Feb = 1,等等),因此+1+2獲取日歷月號。

您可以使用一個JavaScript庫,例如moment.js: http ://momentjs.com/

然后得到這個:

moment().add('months', 1).date(1).subtract('days', 1).day(-4)

我喜歡使用@Aralo建議的諸如moment.js類的抽象。 但是,要在原始JavaScript中執行此操作,您可以使用類似以下的代碼...創建一個可以獲取一個月中所有日期的函數。 然后反向遍歷列表以查找最后一天的編號。 星期三是3

function getDaysInMonth(date) {
  var dayCursor = new Date(today.getFullYear(), today.getMonth()); // first day of month
  var daysInMonth = [];

  while(dayCursor.getMonth() == date.getMonth()) {
    daysInMonth.push(new Date(dayCursor));
    dayCursor.setDate(dayCursor.getDate() + 1);
  }

  return daysInMonth;
}

function findLastDay(date, dayNumber) {
    var daysInMonth = getDaysInMonth(date);
    for(var i = daysInMonth.length - 1; i >= 0; i--) {
      var day = daysInMonth[i];
      if(day.getDay() === dayNumber) return day;
    }
}

然后,獲取當前月份的最后一個星期三:

var today = new Date();
var lastWednesday = findLastDay(today, 3);

這是JS中的一種方法:

var monthLengths = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];

function getLastWednesday() {
  var d = new Date();
  var month = d.getMonth();
  var lastDay = monthLengths[month];
  // mind leap years
  if (month == 1) {
    var year = d.getFullYear();
    var isLeapYear = ((year % 4 == 0 && year % 100 > 0) || year % 400 == 0);
    if (isLeapYear) lastDay++;
  }
  // get the weekday of last day in the curent mont
  d.setDate(lastDay);
  var weekday = d.getDay();
  // calculate return value (wednesday is day 3)
  if (weekday == 3) {
    return lastDay;
  }
  else {
    var offset = weekday - 3;
    if (offset < 0) offset += 7;
    return lastDay - offset;
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM