【问题标题】:Get the dates of the week in javascript在javascript中获取星期几
【发布时间】:2016-12-13 09:39:14
【问题描述】:

我想使用 JavaScript 获取一周中的第一天和最后一天。

例如,今天是 2016-13-12。

FD = 2016-11-12 LD = 2016-17-12

如何获得 FD 和 LD?我尝试使用此代码,

Date.prototype.GetFirstDayOfWeek = function() {
        return (new Date(this.setDate(this.getDate() - this.getDay())));
    }

    Date.prototype.GetLastDayOfWeek = function() {
        return (new Date(this.setDate(this.getDate() - this.getDay() +6)));
    }
    var today = new Date();

它正在工作,但我想将其格式化为“2016-12-11”。 此外,我还想将日期更改为过去或未来。 例如,如果我单击“

场景: 现在的日期是 2016-11-12 - 2016-17-12 当用户单击按钮“

【问题讨论】:

标签: javascript date


【解决方案1】:

您可以使用下面的脚本来查找当周的 firstDay 和 lastDay;

//0=Sunday, 1=Monday
var d = new Date(); 
var day = d.getDay();

var firstDayOW = d.getDate() - day + (day == 0 ? -6:1); 
firstDay = new Date(d.setDate(firstDay));

var lastDayOW = firstDay + 6; 
lastDayOW = new Date(d.setDate(lastDayOW));

希望对你有帮助。

亲切的问候。

【讨论】:

    【解决方案2】:
    var curr = new Date; // get current date
        var first = curr.getDate() - curr.getDay(); // First day is the day of the month - the day of the week
        var last = first + 6; // last day is the first day + 6
    
        var firstday = new Date(curr.setDate(first)).toUTCString();
        var lastday = new Date(curr.setDate(last)).toUTCString();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-08
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 2012-03-29
      相关资源
      最近更新 更多