通过从Javascript中的特定日期减去X天数来查找日期

时间:2022-08-25 18:08:45

I want to find date by subtracting X number of days from a particular date in JavaScript. My JavaScript function accepts 2 parameters. One is the date value and the other is the number of days that needs to be subtracted.

我想通过从JavaScript中的特定日期减去X天数来查找日期。我的JavaScript函数接受2个参数。一个是日期值,另一个是需要减去的天数。

For example, I pass my argument date as 27 July 2009 and i pass my other argument as 3. So i want to calculate the date 3 days before 27 July 2009. So the resultant date that we should get is 24 July 2009. How is this possible in JavaScript. Thanks for any help.

例如,我将我的参数日期定为2009年7月27日,我将我的另一个参数传递给3.所以我想计算2009年7月27日前3天的日期。因此我们得到的日期是2009年7月24日。如何这可能在JavaScript中。谢谢你的帮助。

9 个解决方案

#1


81  

Simply:

只是:

yourDate.setDate(yourDate.getDate() - daysToSubtract);

#2


22  

function date_by_subtracting_days(date, days) {
    return new Date(
        date.getFullYear(), 
        date.getMonth(), 
        date.getDate() - days,
        date.getHours(),
        date.getMinutes(),
        date.getSeconds(),
        date.getMilliseconds()
    );
}

#3


9  

Never go for this solution yourDate.setDate(yourDate.getDate() - daysToSubtract);

永远不要去寻找这个解决方案yourDate.setDate(yourDate.getDate() - daysToSubtract);

it wont work in case your date is 1st of any month and you want to delete some days say 1.

如果您的日期是任何月份的第一天,并且您想要删除某些日子,请不要使用它。

Instead go for below solution which will work always

而是寻求下面的解决方案,它将始终有效

var newDate = new Date( yourDate.getTime() - (days * 24 * 60 * 60 * 1000) );

var newDate = new Date(yourDate.getTime() - (days * 24 * 60 * 60 * 1000));

#4


2  

Here i am posting one more answer and that will return date in specific format.

在这里,我发布了一个答案,并将以特定格式返回日期。

First you can get current date 10/08/2013 as below

首先,您可以获得当前日期10/08/2013,如下所示

function Cureent_Date() {
    var today_GMT = new Date();
    var dd = today_GMT.getDate();
    var mm = today_GMT.getMonth() + 1; //January is 0!
    var yyyy = today_GMT.getFullYear();
    if (dd < 10) {
        dd = '0' + dd
    }
    if (mm < 10) {
        mm = '0' + mm
    }
    current_date = mm + '/' + dd + '/' + yyyy;
    alert("current_date"+current_date);

    Back_date();
}

Now Get back date base on X days

现在回顾X天的日期

function Back_date()
{    
    var back_GTM = new Date(); back_GTM.setDate(back_GTM.getDate() - 2); // 2 is your X
    var b_dd = back_GTM.getDate();
    var b_mm = back_GTM.getMonth()+1;
    var b_yyyy = back_GTM.getFullYear();
    if (b_dd < 10) {
        b_dd = '0' + b_dd
    }
    if (b_mm < 10) {
        b_mm = '0' +b_mm
    }

    var back_date=  b_mm + '/' + b_dd + '/' + b_yyyy;
    alert("back_date"+back_date);
}

So, Today is 10/08/2013 so it will return 10/06/2013.

所以,今天是2013年8月10日所以它将返回10/06/2013。

Check Live Demo here Hope this answer will help you.

在这里查看现场演示希望这个答案能为您提供帮助。

#5


1  

Here's an example, however this does no kind of checking (for example if you use it on 2009/7/1 it'll use a negative day or throw an error.

这是一个例子,但这不做任何检查(例如,如果你在2009/7/1使用它,它将使用负数日或抛出错误。

function subDate(o, days) {
// keep in mind, months in javascript are 0-11
return new Date(o.getFullYear(), o.getMonth(), o.getDate() - days);;
}

#6


0  

This is what I would do. Note you can simplify the expression, I've just written it out to make it clear you are multiplying the number of days by the number of milliseconds in a day.

这就是我要做的。请注意,您可以简化表达式,我只是将其写出来表明您将天数乘以一天中的毫秒数。

 var newDate = new Date( yourDate.getTime() - (days * 24 * 60 * 60 * 1000) );

#7


0  

Just another option, which I wrote:

只是另一种选择,我写道:

DP_DateExtensions Library

DP_DateExtensions库

It's probably overkill if ALL you want to do is one calculation, but if you're going to do more date manipulation you might find it useful.

如果您想要做的只是一次计算,那可能有点过头了,但是如果您要进行更多日期操作,您可能会发现它很有用。

Supports date/time formatting, date math (add/subtract date parts), date compare, date parsing, etc.

支持日期/时间格式,日期数学(加/减日期部分),日期比较,日期解析等。

#8


0  

this is in reference to above answer check this fiddle

这是参考上面的答案检查这个小提琴

https://jsfiddle.net/uniyalguru/azh65aa0/

https://jsfiddle.net/uniyalguru/azh65aa0/

function Cureent_Date() {
var today_GMT = new Date();
var dd = today_GMT.getDate();
var mm = today_GMT.getMonth() + 1; //January is 0!
var yyyy = today_GMT.getFullYear();
if (dd < 10) {
    dd = '0' + dd
}
if (mm < 10) {
    mm = '0' + mm
}
current_date = mm + '/' + dd + '/' + yyyy;

#9


0  

I have created a function for date manipulation. you can add or subtract any number of days, hours, minutes.

我已经创建了一个日期操作功能。您可以添加或减去任意天数,小时数,分钟数。

function dateManipulation(date, days, hrs, mins, operator) {
    date = new Date(date);
    if (operator == "-") {
       var durationInMs = (((24 * days) * 60) + (hrs * 60) + mins) * 60000;
       var newDate = new Date(date.getTime() - durationInMs);
    } else {
       var durationInMs = (((24 * days) * 60) + (hrs * 60) + mins) * 60000;
       var newDate = new Date(date.getTime() + durationInMs);
    }
   return newDate;
}

Now, call this function by passing parameters. For example, here is a function call for getting date before 3 days from today.

现在,通过传递参数来调用此函数。例如,这是一个函数调用,用于从今天起3天之前获取日期。

var today = new Date();
var newDate = dateManipulation(today, 3, 0, 0, "-");

#1


81  

Simply:

只是:

yourDate.setDate(yourDate.getDate() - daysToSubtract);

#2


22  

function date_by_subtracting_days(date, days) {
    return new Date(
        date.getFullYear(), 
        date.getMonth(), 
        date.getDate() - days,
        date.getHours(),
        date.getMinutes(),
        date.getSeconds(),
        date.getMilliseconds()
    );
}

#3


9  

Never go for this solution yourDate.setDate(yourDate.getDate() - daysToSubtract);

永远不要去寻找这个解决方案yourDate.setDate(yourDate.getDate() - daysToSubtract);

it wont work in case your date is 1st of any month and you want to delete some days say 1.

如果您的日期是任何月份的第一天,并且您想要删除某些日子,请不要使用它。

Instead go for below solution which will work always

而是寻求下面的解决方案,它将始终有效

var newDate = new Date( yourDate.getTime() - (days * 24 * 60 * 60 * 1000) );

var newDate = new Date(yourDate.getTime() - (days * 24 * 60 * 60 * 1000));

#4


2  

Here i am posting one more answer and that will return date in specific format.

在这里,我发布了一个答案,并将以特定格式返回日期。

First you can get current date 10/08/2013 as below

首先,您可以获得当前日期10/08/2013,如下所示

function Cureent_Date() {
    var today_GMT = new Date();
    var dd = today_GMT.getDate();
    var mm = today_GMT.getMonth() + 1; //January is 0!
    var yyyy = today_GMT.getFullYear();
    if (dd < 10) {
        dd = '0' + dd
    }
    if (mm < 10) {
        mm = '0' + mm
    }
    current_date = mm + '/' + dd + '/' + yyyy;
    alert("current_date"+current_date);

    Back_date();
}

Now Get back date base on X days

现在回顾X天的日期

function Back_date()
{    
    var back_GTM = new Date(); back_GTM.setDate(back_GTM.getDate() - 2); // 2 is your X
    var b_dd = back_GTM.getDate();
    var b_mm = back_GTM.getMonth()+1;
    var b_yyyy = back_GTM.getFullYear();
    if (b_dd < 10) {
        b_dd = '0' + b_dd
    }
    if (b_mm < 10) {
        b_mm = '0' +b_mm
    }

    var back_date=  b_mm + '/' + b_dd + '/' + b_yyyy;
    alert("back_date"+back_date);
}

So, Today is 10/08/2013 so it will return 10/06/2013.

所以,今天是2013年8月10日所以它将返回10/06/2013。

Check Live Demo here Hope this answer will help you.

在这里查看现场演示希望这个答案能为您提供帮助。

#5


1  

Here's an example, however this does no kind of checking (for example if you use it on 2009/7/1 it'll use a negative day or throw an error.

这是一个例子,但这不做任何检查(例如,如果你在2009/7/1使用它,它将使用负数日或抛出错误。

function subDate(o, days) {
// keep in mind, months in javascript are 0-11
return new Date(o.getFullYear(), o.getMonth(), o.getDate() - days);;
}

#6


0  

This is what I would do. Note you can simplify the expression, I've just written it out to make it clear you are multiplying the number of days by the number of milliseconds in a day.

这就是我要做的。请注意,您可以简化表达式,我只是将其写出来表明您将天数乘以一天中的毫秒数。

 var newDate = new Date( yourDate.getTime() - (days * 24 * 60 * 60 * 1000) );

#7


0  

Just another option, which I wrote:

只是另一种选择,我写道:

DP_DateExtensions Library

DP_DateExtensions库

It's probably overkill if ALL you want to do is one calculation, but if you're going to do more date manipulation you might find it useful.

如果您想要做的只是一次计算,那可能有点过头了,但是如果您要进行更多日期操作,您可能会发现它很有用。

Supports date/time formatting, date math (add/subtract date parts), date compare, date parsing, etc.

支持日期/时间格式,日期数学(加/减日期部分),日期比较,日期解析等。

#8


0  

this is in reference to above answer check this fiddle

这是参考上面的答案检查这个小提琴

https://jsfiddle.net/uniyalguru/azh65aa0/

https://jsfiddle.net/uniyalguru/azh65aa0/

function Cureent_Date() {
var today_GMT = new Date();
var dd = today_GMT.getDate();
var mm = today_GMT.getMonth() + 1; //January is 0!
var yyyy = today_GMT.getFullYear();
if (dd < 10) {
    dd = '0' + dd
}
if (mm < 10) {
    mm = '0' + mm
}
current_date = mm + '/' + dd + '/' + yyyy;

#9


0  

I have created a function for date manipulation. you can add or subtract any number of days, hours, minutes.

我已经创建了一个日期操作功能。您可以添加或减去任意天数,小时数,分钟数。

function dateManipulation(date, days, hrs, mins, operator) {
    date = new Date(date);
    if (operator == "-") {
       var durationInMs = (((24 * days) * 60) + (hrs * 60) + mins) * 60000;
       var newDate = new Date(date.getTime() - durationInMs);
    } else {
       var durationInMs = (((24 * days) * 60) + (hrs * 60) + mins) * 60000;
       var newDate = new Date(date.getTime() + durationInMs);
    }
   return newDate;
}

Now, call this function by passing parameters. For example, here is a function call for getting date before 3 days from today.

现在,通过传递参数来调用此函数。例如,这是一个函数调用,用于从今天起3天之前获取日期。

var today = new Date();
var newDate = dateManipulation(today, 3, 0, 0, "-");