Reputation: 537
I have this script,
var today = new Date();
var dd = today.getDate();
var ddd = today.getDate()-1;
var dddd = today.getDate()-2;
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){
dd='0'+dd
}
if(mm<10){
mm='0'+mm
}
if(ddd<10){
ddd='0'+ddd
}
var today = dd+'/'+mm+'/'+yyyy;
var d2 = ddd+'/'+mm+'/'+yyyy;
var d3 = dddd+'/'+mm+'/'+yyyy;
With this i obtain the last 3 days of the current day but in this case today is 02 if i rest two days i obtain 0 but i want in this case the last day of the previous month, how can do this?
Here is my fiddle
Upvotes: 11
Views: 26097
Reputation: 61
var mydate = new Date(2020,2,1); // march 1st 2020 leap year
var lastdate = new Date(mydate.getFullYear(),mydate.getMonth(),0);// get last date of prev month
var firstdate = new Date(lastdate.getFullYear(), lastdate.getMonth(), 1);//get first date using last date
alert('' + firstdate.toDateString() + '');
alert('' + lastdate.toDateString() + '');
Upvotes: 0
Reputation: 2384
I know many people have already answered it but I just figure out a 3 line solution so I thought I could share it in case anyone needs it.
let date = new Date();
let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 2);
let lastDay = new Date(date.getFullYear(), date.getMonth(), 1);
console.log(firstDay.toISOString(), lastDay.toISOString())
This solution works dynamically even if the current month is January, this method will auto-detect the previous year.
Example:
If the current date is: 2022-01-12
Previous month's first day: 2021-12-01
Previous month's last day: 2021-12-31
**Additionally you can set the time of both dates to 00:00:00
by using setUTCHours
like this:
let date = new Date();
let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 2);
firstDay.setUTCHours(0,0,0,0);
let lastDay = new Date(date.getFullYear(), date.getMonth(), 1);
lastDay.setUTCHours(0,0,0,0);
console.log(firstDay.toISOString(), lastDay.toISOString())
Here's a working Fiddle example in case you need it.
Upvotes: 1
Reputation: 28513
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);
Upvotes: 1
Reputation: 2466
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);
Upvotes: 4
Reputation: 6956
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));
Upvotes: 27
Reputation: 439
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());
Upvotes: 0
Reputation: 3619
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.
Upvotes: 1
Reputation: 171679
A convenient trick to get the end of last month is the take current date and use setDate(0)
. Then use that that to create a new date object and setDate(1)
for beginning of the month
var prevMonthEnd = new Date();
prevMonthEnd.setDate(0);
var beginLastMonth = new Date(prevMonthEnd);
beginLastMonth.setDate(1);
console.log([beginLastMonth.toString(), prevMonthEnd.toString()])
Upvotes: 0
Reputation: 785
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);
Upvotes: 3