Getting the previous month's first date from current date in JavaScript

Javascript

Javascript Problem Overview


Please anyone share the code to find the previous month's first date from current date in JavaScript. For example, if the current date is 25th Jan 2009, I should get 1st Dec 2008 as result.

Javascript Solutions


Solution 1 - Javascript

Straightforward enough, with the date methods:

  var x = new Date();
  x.setDate(1);
  x.setMonth(x.getMonth()-1);

Solution 2 - Javascript

Simplest way would be:

var x = new Date();
x.setDate(0); // 0 will result in the last day of the previous month
x.setDate(1); // 1 will result in the first day of the month

Solution 3 - Javascript

Deals with updating year when moving from January to December

var prevMonth = function(dateObj) {
	var tempDateObj = new Date(dateObj);

	if(tempDateObj.getMonth) {
		tempDateObj.setMonth(tempDateObj.getMonth() - 1);
	} else {
		tempDateObj.setYear(tempDateObj.getYear() - 1);
		tempDateObj.setMonth(12);
	}

	return tempDateObj
};

var wrapper = document.getElementById('wrapper');

for(var i = 0; i < 12; i++) {
	var x = new Date();
  var prevDate = prevMonth(x.setMonth(i));
	var div = document.createElement('div');
  div.textContent = 
  "start month/year: " + i + "/" + x.getFullYear() +
  " --- prev month/year: " + prevDate.getMonth() +
  "/" + prevDate.getFullYear() +
  " --- locale prev date: " + prevDate.toLocaleDateString();
  wrapper.appendChild(div);
}

<div id='wrapper'>
</div>

Solution 4 - Javascript

Important Note: Some of the answers using setMonth() here are wrong:

One liners for use in 2019 (using ES6 syntax; supported by all major browsers and Node):

    const date = new Date().toISOString(); // "2019-09-18T13:49:12.775Z"
    const [yyyy, mm, dd, h, i, s] = date.split(/T|:|-/);


    // previous month's last day
    const prev = new Date(new Date().setDate(0)).toISOString();
    const [pyyyy, pmm] = prev.split(/T|:|-/);

Note that Array destructuring allows you to skip parts:

    const date = new Date().toISOString();
    const [, , dd, , i] = date.split(/T|:|-/);

Explanation: The code above gets the ISO date 2019-09-18T13:49:12.775Z and splits it on : or - or T which returns an array [2019, 09, 18, 13, 49, 12] which then gets destructured.

Using setMonth() is wrong:

date = new Date("Dec 31, 2019")
date.setMonth(date.getMonth() - 1);
date; // Dec 1, 2019!

Solution 5 - Javascript

Check this link:

http://blog.dansnetwork.com/2008/09/18/javascript-date-object-adding-and-subtracting-months/

EDIT: I have drummed up an example:

Date.prototype.SubtractMonth = function(numberOfMonths) {
var d = this;
d.setMonth(d.getMonth() - numberOfMonths);
d.setDate(1);
return d;
}

$(document).ready(function() {
    var d = new Date();
    alert(d.SubtractMonth(1));
});

Solution 6 - Javascript

This worked for me

var curDateMonth = new Date();
var prvDateMonth = new Date(curDateMonth.getFullYear(),curDateMonth.getMonth()-1,curDateMonth.getMonth());
console.log(curDateMonth.toLocaleString('en-US', { month: 'long' }) +' vs '+ prvDateMonth.toLocaleString('en-US', { month: 'long' }));

Solution 7 - Javascript

To get 00:00:00 am of previous month use this:

let d = new Date();
d.setDate(1);
d.setMonth(d.getMonth() - 1);
d.setHours(0,0,0,0);
const lastMonthStart = new Date(d);

Hope this helps!!

Solution 8 - Javascript

//  Previous Month Detail
let prevStartDate = new Date(this.date.getFullYear(), this.date.getMonth() - 1, 1);
console.log(prevStartDate);
let preEndDate = new Date(this.date.getFullYear(), this.date.getMonth() - 1 + 1, 0);
console.log(preEndDate);
//Current Month Detail
let cStartDate = new Date(this.date.getFullYear(), this.date.getMonth(), 1);
console.log(cStartDate);
let cEndDate = new Date(this.date.getFullYear(), this.date.getMonth(), 1, 0);
console.log(cEndDate);
//Next Month Detail
let nStartDate = new Date(this.date.getFullYear(), this.date.getMonth() + 1, 1);
console.log(nStartDate);
let nendDate = new Date(this.date.getFullYear(), this.date.getMonth() + 1 + 1, 0);
console.log(nendDate);

Solution 9 - Javascript

//just try this will work fine

let date = new Date();
let month = new Date().getMonth();
let prevMonth = date.setMonth(month - 1)
let formatPrevMonth = new Date(date.setMonth(month - 1));

console.log(formatPrevMonth)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGeethaView Question on Stackoverflow
Solution 1 - JavascriptannakataView Answer on Stackoverflow
Solution 2 - JavascriptReza GhorbaniView Answer on Stackoverflow
Solution 3 - JavascriptgoogamangaView Answer on Stackoverflow
Solution 4 - JavascriptaleembView Answer on Stackoverflow
Solution 5 - JavascriptREA_ANDREWView Answer on Stackoverflow
Solution 6 - Javascriptuser7162127View Answer on Stackoverflow
Solution 7 - JavascriptHarshit AgarwalView Answer on Stackoverflow
Solution 8 - JavascriptArjunan AjevarView Answer on Stackoverflow
Solution 9 - Javascriptahmed mersalView Answer on Stackoverflow