[英]Last week start date and end date using javascript not giving required result
I am using the following code, but it is returning last week's 我正在使用以下代码,但返回的是上周的
start date = 0-4/09/2018 and 开始日期= 0-4 / 09/2018和
end date = 02/09/2018 结束日期= 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. 在获取值之前,必须使用Date对象并将其日期设置为所需的值。 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. 在建议使用moment.js
进行日期/时间操作时,我提供了一个基于此的解决方案。
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.