get first and last date of current month with JavaScript

 

To get the first day of a month, use the Date() constructor to create a date object, passing it the year, month and 1 for the day as parameters. The Date object will contain the first day of the month.

var date = new Date();
var firstDayOfMonth  = new Date(date.getFullYear(), date.getMonth(), 1);
var lastDayOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0);

 

To get the last day of a month, use the Date() constructor to create a date object, passing it the year, month and date.getMonth() for the day as parameters. The Date object will contain the last day of the month.

 


var lastDayOfMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0);

Tags:

Share:

Related posts