我在今天日期之前12天计算。但它没有返回正确的日期。 例如,对于今天的数据,11/11/2013(mm / dd / yyyy),当它应该返回10/31/2013时,它将返回2013年10月30日。
这是代码
var d = new Date();
d.setDate(d.getDate() - 12);
d.setMonth(d.getMonth() + 1 - 0);
var curr_date = d.getDate();
var curr_month = d.getMonth();
var curr_year = d.getFullYear();
if (curr_month < 10 && curr_date < 10) {
var parsedDate = "0" + curr_month + "/" + "0" + curr_date + "/" + curr_year;
alert(parsedDate);
} else if (curr_month < 10 && curr_date > 9) {
var parsedDate = "0" + curr_month + "/" + curr_date + "/" + curr_year;
alert(parsedDate);
} else if (curr_month > 9 && curr_date < 10) {
var parsedDate = curr_month + "/" + "0" + curr_date + "/" + curr_year;
alert(parsedDate);
} else {
var parsedDate = curr_month + "/" + curr_date + "/" + curr_year;
alert(parsedDate);
}
答案 0 :(得分:26)
问题解决了
var days; // Days you want to subtract
var date = new Date();
var last = new Date(date.getTime() - (days * 24 * 60 * 60 * 1000));
var day =last.getDate();
var month=last.getMonth()+1;
var year=last.getFullYear();
答案 1 :(得分:10)
试图减去天数很棘手。最好从时间戳中减去并更改日期。
要减去12天,请执行:
var d = new Date();
var ts = d.getTime();
var twelveDays = ts - (12 * 24 * 60 * 60 * 1000);
d.setUTCDate(twelveDays);
答案 2 :(得分:7)
您可以使用以下代码获取从今天到之前7天的日期
var date = new Date();
date.setDate(date.getDate() - 7);
var finalDate = date.getDate()+'/'+ (date.getMonth()+1) +'/'+date.getFullYear();
答案 3 :(得分:1)
binary(0, length)
新日期-从计算的毫秒时间创建日期对象。
Date.now()-给出从1970年开始的时间,以毫秒为单位。
7 * 24 * 60 * 60 * 1000-7天(以毫秒为单位)。
答案 4 :(得分:1)
要获取过去的日期,请使用此代码
查看控制台以获取结果
const GetDays = (d,Mention_today=false)=>{
//Mention today mean the array will have today date
var DateArray = [];
var days=d;
for(var i=0;i<days;i++){
if(!Mention_today && i==0){i=1;days+=1}
var date = new Date();
var last = new Date(date.getTime() - (i * 24 * 60 * 60 * 1000));
var day =last.getDate();
var month=last.getMonth()+1;
var year=last.getFullYear();
const fulld = (Number(year)+'-'+Number(month)+'-'+Number(day)) // Format date as you like
DateArray.push(fulld);
}
return DateArray;
}
console.log(GetDays(5)) //Will get the past 5 days formated YY-mm-dd
答案 5 :(得分:1)
Date.prototype.addDays = function(days) {
// Add days to given date
var date = new Date(this.valueOf());
date.setDate(date.getDate() + days);
return date;
}
let today = new Date()
console.log(today.addDays(-7))
答案 6 :(得分:1)
首先:获取当前日期
const startingDate = new Date();
第二:获取你想要的日期 !!:如果直接使用setDate改变startingDate,会改变这个变量。
const sevenDaysBeforeDate = new Date(new Date().setDate(new Date().getDate() - 7));
7 天后
const endDate = new Date(new Date().setDate(new Date().getDate() + 7));
答案 7 :(得分:0)
这是一个根据以下内容返回过去或将来的日期的函数;
If plusMinus = -1 then Past Date
If plusMinus = 1 then Future Date
function getDate(inDays, plusMinus) {
const today = new Date();
return new Date(today.getFullYear(),
today.getMonth(),
today.getDate() + (inDays * plusMinus));
}
答案 8 :(得分:0)
已更新:)
var timeFrom = (X) => {
var dates = [];
for (let I = 0; I < Math.abs(X); I++) {
dates.push(new Date(new Date().getTime() - ((X >= 0 ? I : (I - I - I)) * 24 * 60 * 60 * 1000)).toLocaleString());
}
return dates;
}
console.log(timeFrom(-7)); // Future 7 Days
console.log(timeFrom(7)); // Past 7 Days
输出
[
'7/26/2019, 3:08:15 PM',
'7/27/2019, 3:08:15 PM',
'7/28/2019, 3:08:15 PM',
'7/29/2019, 3:08:15 PM',
'7/30/2019, 3:08:15 PM',
'7/31/2019, 3:08:15 PM',
'8/1/2019, 3:08:15 PM'
]
[
'7/26/2019, 3:08:15 PM',
'7/25/2019, 3:08:15 PM',
'7/24/2019, 3:08:15 PM',
'7/23/2019, 3:08:15 PM',
'7/22/2019, 3:08:15 PM',
'7/21/2019, 3:08:15 PM',
'7/20/2019, 3:08:15 PM'
]
答案 9 :(得分:0)
使用dayjs库,我们可以更轻松地完成
。import dayjs from 'dayjs';
const getDate = (prevDays) => {
const now = dayjs();
console.log(now.subtract(prevDays, 'day').format('mm-dd-yyyy'));
return now.subtract(prevDays, 'day').toDate();
}
答案 10 :(得分:0)
这是最好的解决方案..当X是您的日子:
var dates = [];
for (let i = 0; i < X; i++) {
var date = new Date();
var thatDay = date.getDate() - i; //Current Date
date.setDate(thatDay);
let day = date.getDate();
let month = date.getMonth() + 1;
let year = date
.getFullYear()
.toString()
.substr(-2);
dates.push(month + '/' + day + '/' + year); //format it as you need
}
//output mm/d/yy
答案 11 :(得分:-1)
感谢您的帮助。我只是用了一个简单的函数,效果很好
const subtractDayFromDate = (date, days) => {
const newDate = new Date(date);
newDate.setDate(newDate.getDate() - days);
return newDate;
};