我正试图形成一个日期,这是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)吗?
当前回答
传递一个JS Date对象和一个你想加/减多少个月的整数。monthsToAdd可以是正的也可以是负的。返回一个JS日期对象。
如果您的originalDateObject是3月31日,并且您传递-1作为monthsToAdd,那么您的输出日期将是2月28日。
如果您经过了大量的月份,比如36个月,它也会正确地处理年份调整。
const addMonthsToDate = (originalDateObject, monthsToAdd) => {
const originalDay = originalDateObject.getUTCDate();
const originalMonth = originalDateObject.getUTCMonth();
const originalYear = originalDateObject.getUTCFullYear();
const monthDayCountMap = {
"0": 31,
"1": 28,
"2": 31,
"3": 30,
"4": 31,
"5": 30,
"6": 31,
"7": 31,
"8": 30,
"9": 31,
"10": 30,
"11": 31
};
let newMonth;
if (newMonth > -1) {
newMonth = (((originalMonth + monthsToAdd) % 12)).toString();
} else {
const delta = (monthsToAdd * -1) % 12;
newMonth = originalMonth - delta < 0 ? (12+originalMonth) - delta : originalMonth - delta;
}
let newDay;
if (originalDay > monthDayCountMap[newMonth]) {
newDay = monthDayCountMap[newMonth].toString();
} else {
newDay = originalDay.toString();
}
newMonth = (+newMonth + 1).toString();
if (newMonth.length === 1) {
newMonth = '0' + newMonth;
}
if (newDay.length === 1) {
newDay = '0' + newDay;
}
if (monthsToAdd <= 0) {
monthsToAdd -= 11;
}
let newYear = (~~((originalMonth + monthsToAdd) / 12)) + originalYear;
let newTime = originalDateObject.toISOString().slice(10, 24);
const newDateISOString = `${newYear}-${newMonth}-${newDay}${newTime}`;
return new Date(newDateISOString);
};
其他回答
对于今天之前三个月的获取日期:
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)
我建议使用一个名为Moment.js的库。
它经过了良好的测试,可以跨浏览器和服务器端工作(我在Angular和Node项目中都使用它)。它对区域日期有很好的支持。
http://momentjs.com/
var threeMonthsAgo = moment().subtract(3, 'months');
console.log(threeMonthsAgo.format()); // 2015-10-13T09:37:35+02:00
.format()返回ISO 8601格式的日期字符串表示形式。你也可以像这样使用自定义日期格式:format('dddd, MMMM Do YYYY, h:mm:ss a')
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 minDate = new Date();
minDate.setMonth(minDate.getMonth() - 3);
声明具有当前日期的变量。 然后通过使用setMonth内置函数,我们可以得到3个月后的日期。
这样做
let currentdate = new Date();
let last3months = new Date(currentdate.setMonth(currentdate.getMonth()-3));
Javascript的setMonth方法也负责年份。例如,如果currentDate设置为new Date("2020-01-29"),上述代码将返回2020-01-29。