Get first and last date of current month in JavaScript – Any given month also

This is a JavaScript tutorial where you can learn how to get first and last date of the current month in JavaScript. In many projects, we need to find out the first and last date. So I thought this tutorial gonna be helpful for us.
To get first and last date of current month in JavaScript we need these below methods:

  • getFullYear()
  • getMonth()

You may also read this,

How to subtract minutes from date in JavaScript
How to add months to a date in JavaScript

In this tutorial, we will learn to find for both the current month and any given month

get first and last date of current month in javascript

Get first and last date of current month in JavaScript

var my_date = new Date();
var first_date = new Date(my_date.getFullYear(), my_date.getMonth(), 1);
document.write(first_date);
var last_date = new Date(my_date.getFullYear(), my_date.getMonth() + 1, 0);
document.write("<br>"+last_date);

Output:

Thu Nov 01 2018 00:00:00 GMT+0530 (India Standard Time)
Fri Nov 30 2018 00:00:00 GMT+0530 (India Standard Time)

This is for the current date.



If you want to input the date manually you can insert the custom date in the: var my_date = new Date();

Date(); – here you can pass your custom date as a parameter.

Get first and last day of any given month

Now suppose that you want to get the first and last day of a particular given month. Let’s assume that we need to find the first day and the last day of January month of 2018.

var my_date = new Date("2018, Jan"); 
var first_date = new Date(my_date.getFullYear(), my_date.getMonth(), 1); 
document.write(first_date); 
var last_date = new Date(my_date.getFullYear(), my_date.getMonth() + 1, 0); 
document.write("<br>"+last_date);

Output:

Mon Jan 01 2018 00:00:00 GMT+0530 (India Standard Time)
Wed Jan 31 2018 00:00:00 GMT+0530 (India Standard Time)

If you have any further doubts or questions put that in the below comment section.

 

Leave a Reply

Your email address will not be published. Required fields are marked *