1

Can any one help me to find first date and last date from month and year. I have 2 parameter month and year, for example month = 10 and year = 2021

I tried some code but it doesn't have the answer

var prevMonthLastDay = new Date(2021, 10, 0);
var thisMonthFirstDay = new Date(2021, 10, 1);
console.log(prevMonthLastDay);
console.log(thisMonthFirstDay);

Above code is giving following output:

2021-10-30T17:00:00.000Z
2021-10-31T17:00:00.000Z 

My expected answer is first date 2021-10-01 and last date 2021-10-31

riyan193
  • 53
  • 7
  • Months are zero indexed, so the first expression will give the 0th day of November, which is the last day of October. But because you're timezone appears to be -7, the UTC date (note the Z at the end of the timestamp) is 17:00 (5 pm) the previous day, so you get 30 October. The second expression just returns 1 November (because months are zero indexed), so subtract 7 hours again to get UTC and you get 17:00 on 31 October. – RobG Oct 24 '21 at 04:16

5 Answers5

1

You can try this way

var date = new Date();
var firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
khairul alam
  • 138
  • 13
  • can you explain i have parameter month and year example month = 10 and year 2021 thanks – riyan193 Oct 21 '21 at 05:27
  • follow this link. https://stackoverflow.com/questions/1924815/how-to-get-last-day-of-the-month/1924846#1924846 – khairul alam Oct 21 '21 at 05:36
  • If the answer is given at another link, then this is a duplicate and you should mark it as such rather than answering. – RobG Oct 24 '21 at 04:17
1

Try the following to get your expected output:

function GetFirstAndLastDate(year, month)
{
var firstDayOfMonth = new Date(year, month-1, 2);
var lastDayOfMonth = new Date(year, month, 1);

console.log('First Day: ' + firstDayOfMonth.toISOString().substring(0, 10));
console.log('Last Day: ' + lastDayOfMonth.toISOString().substring(0, 10));

}

GetFirstAndLastDate(2021, 10);
1

I'm a little bit confused with what you really want to get referencing your variable names, so I include them all:

const currentYear = 2021;
const currentMonth = 10;

const prevMonthFirstDay = new Date(currentYear, currentMonth - 2, 1);
const prevMonthLastDay = new Date(currentYear, currentMonth - 1, 0);

const currentMonthFirstDay = new Date(currentYear, currentMonth - 1, 1);
const currentMonthLastDay = new Date(currentYear, currentMonth, 0);

const nextMonthFirstDay = new Date(currentYear, currentMonth, 1);
const nextMonthLastDay = new Date(currentYear, currentMonth + 1, 0);

console.log('prev month first day:', prevMonthFirstDay);
console.log('prev month last day:', prevMonthLastDay);
console.log('current month first day:', currentMonthFirstDay);
console.log('current month last day:', currentMonthLastDay);
console.log('next month first day:', nextMonthFirstDay);
console.log('next month last day:', nextMonthLastDay);

on my system this will gives:

prev month first day: Wed Sep 01 2021 00:00:00 GMT+0700 (Western Indonesia Time)
prev month last day: Thu Sep 30 2021 00:00:00 GMT+0700 (Western Indonesia Time)
current month first day: Fri Oct 01 2021 00:00:00 GMT+0700 (Western Indonesia Time)
current month last day: Sun Oct 31 2021 00:00:00 GMT+0700 (Western Indonesia Time)
next month first day: Mon Nov 01 2021 00:00:00 GMT+0700 (Western Indonesia Time)
next month last day: Tue Nov 30 2021 00:00:00 GMT+0700 (Western Indonesia Time)

so now it will only a matter of formatting:

console.log(currentMonthFirstDay.toLocaleDateString('en-GB').split('/').reverse().join('-'));

which will give you:

2021-10-01
widyakumara
  • 1,065
  • 1
  • 9
  • 14
0

You can follow this.

//first and last of the current month
var current_month = "April 2017";
var arrMonth = current_month.split(" ");
var first_day = new Date(arrMonth[0] + " 1 " + arrMonth[1]);

//even though I already have the values, I'm using date functions to get year and month
//because month is zero-based
var last_day = new Date(first_day.getFullYear(), first_day.getMonth() + 1, 0, 23, 59, 59);

//use moment,js to format       
var start = moment(first_day).format("YYYY-MM-DD");
var end = moment(last_day).format("YYYY-MM-DD");

the answer is in the following link: How to get last day of the month

khairul alam
  • 138
  • 13
0

You can try this way referrence

var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);