I am trying to set start and end date in date picker where start date is week before current date and end date is week after start date. In certain condition it gives me 0 for start date. Can anyone look into code below and help me to get proper date range when current date is first day of the month or last date of the month. Thanks for your help.
var date = new Date();
//When current date was less then 7 in some situation it giving me error. So, I am checking current date and randomly subtract 3. //if current date is less then 7 then get the last day of the previous month if (date.getDate() <= 7) {
day = new Date(date.getFullYear(), date.getMonth(), 0);
startdate = day.getDate() - 3;
month = day.getMonth() + 1;
year = day.getFullYear();
if (month < 10) month = "0" + month;
if (day < 10) day = day;
console.log("App Config: lasy day: " + startdate + "\nmonth: " + month + "\nyear:" + year);
}
else {
startdate = date.getDate() - 7;
month = date.getMonth() + 1;
year = date.getFullYear();
if (month < 10) month = "0" + month;
if (day < 10) day = day;
console.log("App Config: day: " + startdate + "\nmonth: " + month + "\nyear:" + year);
}
return month + "/" + startdate + "/" + year;
date.setDate(date.getDate() - 7)it will figure it out automatically.