You can use the Date
object in JavaScript to find the first day of the month for a given date. Here's an example:
function getFirstDayOfMonth(date) {
// Create a new Date object with the given date
const currentMonth = new Date(date);
// Set the date to the first day of the month
currentMonth.setDate(1);
// Return the first day of the month
return currentMonth;
}
// Example usage
const inputDate = new Date('2022-06-15');
const firstDayOfMonth = getFirstDayOfMonth(inputDate);
console.log(firstDayOfMonth);
In this example, the getFirstDayOfMonth
function takes a date
parameter and creates a new Date
object using the given date. It then uses the setDate
method to set the date to 1, which represents the first day of the month. The function returns the first day of the month.
You can replace the inputDate
with any valid date string or a Date
object to get the first day of the month for that date.