Getting the previous month's first date from current date in JavaScript
JavascriptJavascript 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.
setMonth()
is wrong:
Using 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)