我正试图形成一个日期,这是3个月前的当前日期。我通过下面的代码得到当前月份
var currentDate = new Date();
var currentMonth = currentDate.getMonth()+1;
你能给我提供一个逻辑来计算和形成一个日期(date数据类型的对象),考虑到当月份是一月(1)时,日期前3个月将是十月(10)吗?
我正试图形成一个日期,这是3个月前的当前日期。我通过下面的代码得到当前月份
var currentDate = new Date();
var currentMonth = currentDate.getMonth()+1;
你能给我提供一个逻辑来计算和形成一个日期(date数据类型的对象),考虑到当月份是一月(1)时,日期前3个月将是十月(10)吗?
当前回答
对于今天之前三个月的获取日期:
let d = new Date(new Date().setMonth(new Date().getMonth() - 3))
console.log(d.toISOString().slice(0, 10))
// 2022-05-24 (today is 2022-08-24)
其他回答
我喜欢gilly3的简单回答,但用户可能会惊讶于3月31日之前的一个月是3月3日。我选择实现一个坚持月底的版本,所以3月28日、29日、30日和31日之前的一个月都是2月28日,因为不是闰年。
function addMonths(date, months) { var result = new Date(date), expectedMonth = ((date.getMonth() + months) % 12 + 12) % 12; result.setMonth(result.getMonth() + months); if (result.getMonth() !== expectedMonth) { result.setDate(0); } return result; } var dt2004_05_31 = new Date("2004-05-31 0:00"), dt2001_05_31 = new Date("2001-05-31 0:00"), dt2001_03_31 = new Date("2001-03-31 0:00"), dt2001_02_28 = new Date("2001-02-28 0:00"), result = addMonths(dt2001_05_31, -2); console.assert(dt2001_03_31.getTime() == result.getTime(), result.toDateString()); result = addMonths(dt2001_05_31, -3); console.assert(dt2001_02_28.getTime() == result.getTime(), result.toDateString()); result = addMonths(dt2001_05_31, 36); console.assert(dt2004_05_31.getTime() == result.getTime(), result.toDateString()); result = addMonths(dt2004_05_31, -38); console.assert(dt2001_03_31.getTime() == result.getTime(), result.toDateString()); console.log('Done.');
Following code give me Just Previous Month From Current Month even the date is 31/30 of current date and last month is 30/29/28 days:
<!DOCTYPE html>
<html>
<body>
<p>Click the button to display the date after changing the month.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() {
var d = new Date("March 29, 2017"); // Please Try the result also for "March 31, 2017" Or "March 30, 2017"
var OneMonthBefore =new Date(d);
OneMonthBefore.setMonth(d.getMonth(),0);
if(OneMonthBefore.getDate() < d.getDate() )
{
d.setMonth(d.getMonth(),0);
}else
{
d.setMonth(d.getMonth()-1);
}
document.getElementById("demo").innerHTML = d;
}
</script>
</body>
</html>
var日期= . getelementbyid(“日期”); var d = new Date(); 文档。写(d + "<br/>"); d.setMonth(d.getMonth() - 6); document . write (d); 如果(d <日期) 文档。写作(“少于6个月”); 其他的 文档。写(“大于6个月”);
这样做
let currentdate = new Date();
let last3months = new Date(currentdate.setMonth(currentdate.getMonth()-3));
Javascript的setMonth方法也负责年份。例如,如果currentDate设置为new Date("2020-01-29"),上述代码将返回2020-01-29。
在我的例子中,我需要减去1个月到当前日期。重要的部分是月号,所以它不关心你在本月的哪一天,我需要上个月。这是我的代码:
var dateObj = new Date('2017-03-30 00:00:00'); //Create new date object
console.log(dateObj); // Thu Mar 30 2017 00:00:00 GMT-0300 (ART)
dateObj.setDate(1); //Set first day of the month from current date
dateObj.setDate(-1); // Substract 1 day to the first day of the month
//Now, you are in the last month
console.log(dateObj); // Mon Feb 27 2017 00:00:00 GMT-0300 (ART)
减去1个月到实际日期是不准确的,这就是为什么在第一个地方我设置了一个月的第一天(任何一个月的第一天总是第一天),在第二个地方我减去1天,这总是把你送到上个月。 希望能帮到你,伙计。
var dateObj = new Date('2017-03-30 00:00:00');//创建新的日期对象 console.log (dateObj);// 2017年3月30日星期四00:00:00 GMT-0300 (ART) dateObj.setDate (1);//从当前日期开始设置月的第一天 dateObj.setDate (1);//每月的第一天减1 //现在是最后一个月 console.log (dateObj);// 2017年2月27日星期一00:00:00 GMT-0300 (ART)