I am using the following code, but it is returning last week's
start date = 0-4/09/2018 and
end date = 02/09/2018
var curr = new Date;
var first = curr.getDate() - curr.getDay();
var last = first + 6;
var startDate = new Date(curr.setDate(first));
var dd1 = startDate.getDate()-6;
var mm1 = startDate.getMonth() + 1;
if(dd1<10){
dd1='0'+dd1
}
if(mm1<10){
mm1='0'+mm1
}
startDate = "" + dd1 + "/" + mm1 + "/" + startDate.getFullYear();
var endDate = new Date(curr.setDate(last));
var dd2 = endDate.getDate()-6;
var mm2 = endDate.getMonth() + 1;
if(dd2<10){
dd2='0'+dd2
}
if(mm2<10){
mm2='0'+mm2
}
endDate = "" + dd2 + "/" + mm2 + "/" + endDate.getFullYear();
You have to use the Date object and set its date to the value you like before getting the value. Your implementation is not valid because:
The correct form is that:
var curr,startDate,endDate, strStartDate,strEndDate;
curr = new Date();
curr.setDate(curr.getDate()+6)
startDate = new Date(curr.getTime());
startDate.setDate(startDate.getDate()-6)
strStartDate = startDate.getDate()+"/"+(startDate.getMonth()+1)+"/"+startDate.getFullYear();
endDate = new Date(startDate.getTime());
endDate.setDate(endDate.getDate()-6);
strEndDate = endDate.getDate()+"/"+(endDate.getMonth()+1)+"/"+endDate.getFullYear();
With a recommendation to use moment.js
for date/time manipulation, I offer a solution based on it.
let startDate = moment().endOf("month").startOf("isoWeek"); // Also, check .startOf("week");
let endDate = moment().endOf("month").endOf("isoWeek"); // Also, check .endOf("week");
console.log(start); // moment object
console.log(start.format('DD-MM-YYYY')); // date string
console.log(end); // moment object
console.log(end.format('DD-MM-YYYY')); // date string
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.