I have this script,
var today = new Date();
var dd = today.getDate();
var ffffd = today.getDate()-1;
var ffffdd = today.getDate()-2;
var mm = today.getMonth()+
That's the first day of the current month new Date(now.getFullYear(), now.getMonth(), 1)
to get the last day of the previous month create a date 1-day earlier: new Date(now.getFullYear(), now.getMonth(), 1 - 1)
.
To get the first day of the previous month we should substract 1 from the month component new Date(now.getFullYear(), now.getMonth() - 1, 1)
but there is an issue if the current month is January (0) and the previous month is December (11). Hence I wrapped the month expression creating a cycle so it always returns a positiove value.
var now = new Date();
var prevMonthLastDate = new Date(now.getFullYear(), now.getMonth(), 0);
var prevMonthFirstDate = new Date(now.getFullYear() - (now.getMonth() > 0 ? 0 : 1), (now.getMonth() - 1 + 12) % 12, 1);
var formatDateComponent = function(dateComponent) {
return (dateComponent < 10 ? '0' : '') + dateComponent;
};
var formatDate = function(date) {
return formatDateComponent(date.getMonth() + 1) + '/' + formatDateComponent(date.getDate()) + '/' + date.getFullYear();
};
document.write(formatDate(prevMonthFirstDate) + ' - ' + formatDate(prevMonthLastDate));
var firstDay = new Date();
var secondDay = new Date();
var thirdDay = new Date();
//Now say you want to set secondDay as before 2 days
secondDay.setDate(secondDay.getDate()-2);
//Now say you want to set thirdDay as after 2 days
thirdDay.setDate(thirdDay.getDate()+2);
alert("firstDay: "+firstDay+", secondDay:"+secondDay+", thirdDay:"+thirdDay);
alert("date of thirdDay: dd/mm/yyyy "+thirdDay.getDate()+"/"+ (thirdDay.getMonth()+1) +"/"+thirdDay.getFullYear());
You can do it like this
//one day previous Date
var date = new Date();
date.setDate(date.getDate() - 1);
console.log(date.getDay());
//for previous month last date
var date = new Date();
date.setDate(0);
console.log(date);
//for perivous Month First date
var date = new Date();
date.setDate(0);
date.setDate(1);
console.log(date);
What I have guessed is if current month is February then your output should be 1,31
because January's first day and last day is 1 and 31. So if that is the case then
var toDay = new Date();
var myDate = new Date(2016,toDay.getMonth()-1);
Fri Jan 01 2016 00:00:00 GMT+0530 (India Standard Time)
This will be assigned with the first day of the previous month.
Also, for the second case
var myDate = new Date(2016, toDay.getMonth());
myDate = new Date(myDate -1);
Sat Jan 31 2015 23:59:59 GMT+0530 (India Standard Time)
You can just use myDate
to find whatever you want.
Try this: get today's date and set date to 1 which will get first of the current month. Then set hour to -1 which will shift it to previous months last day. Then set day to 1 to get first day of previous month.
var today = new Date();
var dd = today.getDate();
today.setDate(1); // going to 1st of the month
today.setHours(-1); // going to last hour before this date even started.
var lastDay = today.toLocaleDateString('en-GB', {
year: 'numeric',
month: 'numeric',
day: 'numeric'
}).split(' ').join('-');
alert("last day of previous month " + lastDay);
today.setDate(1); // going to 1st of the previous month
var firstDay = today.toLocaleDateString('en-GB', {
year: 'numeric',
month: 'numeric',
day: 'numeric'
}).split(' ').join('-');
alert("first day of previous month " + firstDay);
JSFiddle Demo
Try using bellow
var date = new Date();
var monthStartDay = new Date(date.getFullYear(), date.getMonth(), 1);
var monthEndDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);