Get the first and last day of the previous month jquery

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 get the last 3 days of the current day, but in this case today 02, if I rest for two days, I get 0, but in this case I want the last day of the previous month, how can this be done?

Here is my fiddle

+5
source share
7 answers

To make 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 must subtract 1 from the component of the month new Date(now.getFullYear(), now.getMonth() - 1, 1) , but there is a problem if the current month is January (0) and the previous month - December (11). So I wrapped the month expression by creating a loop so that it always returns the 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)); 
+6
source

Try using below.

 var date = new Date(); var monthStartDay = new Date(date.getFullYear(), date.getMonth(), 1); var monthEndDay = new Date(date.getFullYear(), date.getMonth() + 1, 0); 
+2
source

Try it: get the date today and set the date to 1, which will be the first in the current month. Then set the hour to -1, which will move it to previous months on the last day. Then set day 1 to get the first day of the 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

+1
source

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); 
+1
source

I assumed that if the current month is February, then your conclusion should be 1,31 , because the first day of January and the last day are 1 and 31. So, if that is so, 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 on the first day of the previous month.

In addition, 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 simply use myDate to find whatever you want.

+1
source

A convenient trick to get the end of the last month is the current date and using setDate(0) . Then use this to create a new date object and setDate(1) to start the month

 var prevMonthEnd = new Date(); prevMonthEnd.setDate(0); var beginLastMonth = new Date(prevMonthEnd); beginLastMonth.setDate(1); console.log([beginLastMonth.toString(), prevMonthEnd.toString()]) 
0
source

  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()); 
0
source

Source: https://habr.com/ru/post/1242152/


All Articles