JavaScript计算日期从今天到7天

ozi*_*zil 11 javascript date

我在今天日期之前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);
}
Run Code Online (Sandbox Code Playgroud)

ozi*_*zil 32

问题解决了

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();
Run Code Online (Sandbox Code Playgroud)


Sch*_*eis 11

试图减去天数是棘手的.最好从时间戳中减去并更改日期.

减去12天做:

   var d = new Date();
   var ts = d.getTime();
   var twelveDays = ts - (12 * 24 * 60 * 60 * 1000);
   d.setUTCDate(twelveDays);
Run Code Online (Sandbox Code Playgroud)


小智 9

您可以使用以下代码获取从今天到7天之前的日期

var date = new Date();
date.setDate(date.getDate() - 7);

var finalDate = date.getDate()+'/'+ (date.getMonth()+1) +'/'+date.getFullYear();
Run Code Online (Sandbox Code Playgroud)


RTW*_*RTW 6

纯js一线解决方案:

new Date(Date.now() - 7 * 24 * 60 * 60 * 1000)
Run Code Online (Sandbox Code Playgroud)
  1. new Date()-从计算的毫秒时间创建Date对象。
  2. Date.now()-给出从1970年开始的时间(以毫秒为单位)。
  3. 7(天)* 24(小时)* 60(分钟)* 60(秒)* 1000(毫秒)= 604800000或7天(以毫秒为单位)。

如果您没有计划更改已扣除的值,或者可以为轻松更改已扣除的天数,分钟数等进行计算,则可以使用已计算的值。


如果您打算更频繁地使用日期和时间,我建议使用moment.js

moment().subtract(7, 'days').calendar()
Run Code Online (Sandbox Code Playgroud)


Chr*_*ley 6

更新 :)

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
Run Code Online (Sandbox Code Playgroud)

输出

[
  '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'
]
Run Code Online (Sandbox Code Playgroud)


小智 5

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))
Run Code Online (Sandbox Code Playgroud)