繁体   English   中英

在JavaScript中获取星期几

[英]Get the dates of the week in javascript

我想使用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”中设置其格式。 我也想将日期更改为过去或将来。 例如,如果我单击按钮“ <”,它将超过1周。 然后,如果用户再次单击该按钮,它将获得下一个上周。 等等。

场景:日期现在为2016-11-12-2016-17-12当用户单击按钮“ <”时。 它将成为2016-04-12-2016-10-12

您可以使用以下脚本查找当前星期的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));

我希望这可以帮助你。

亲切的问候。

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();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM