我正试图形成一个日期,这是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)吗?
当前回答
在我的例子中,我需要减去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)
其他回答
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>
我喜欢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.');
传递一个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);
};
如果gilly3提供的setMonth方法不是你想要的,可以考虑:
var someDate = new Date(); // add arguments as needed
someDate.setTime(someDate.getTime() - 3*28*24*60*60);
// assumes the definition of "one month" to be "four weeks".
可以使用任何数量的时间,只要设置正确的倍数。
直接放入变量中的“一行代码”(多行代码,便于阅读):
var oneMonthAgo = new Date(
new Date().getFullYear(),
new Date().getMonth() - 1,
new Date().getDate()
);