Subtracting days/months/years from a Date object

后端 未结 4 1736
我寻月下人不归
我寻月下人不归 2021-01-04 09:06
var inputDate     = \'20/4/2010\'.split(\'/\');
var dateFormatted = new Date(parseInt(inputDate[2]), parseInt(inputDate[1]), parseInt(inputDate[0]));

var expiryDate         


        
相关标签:
4条回答
  • 2021-01-04 09:19

    A similar question has been answered here:

    How to add/subtract dates with javascript?

    Similar thing can be done for months and years.

    For e.g.

         var date = new Date('2011','01','02');
         alert('the original date is '+date);
         var newdate = new Date(date);
         newdate.setMonth(newdate.getMonth() - 7);
         var nd = new Date(newdate);
         alert('the new date is '+nd);
    
    0 讨论(0)
  • 2021-01-04 09:19
    var currentDate = new Date(year,month,day);
    var expiryDate = new Date();
    expiryDate.setTime(currentDate.getTime() + (3 * 365 * 24 * 60 * 60 * 1000));
    

    using the number of seconds past 1970 is fine for this :-) oh, you have more rules. well after that you will still have to check for those cases...

    0 讨论(0)
  • 2021-01-04 09:34

    Maybe this will be useful to you: http://code.google.com/p/datejs/wiki/APIDocumentation

    0 讨论(0)
  • 2021-01-04 09:39

    Easy way to see if a date inputed is a valid date:

    var d = Date.parse('4/20/2010');
    if (isNaN(d.valueOf())) {
     alert ("bad date value"); 
    }
    

    Then, here is a dateAdd function that I use regularly. Extends the Date object, so it's easy to use:

    Date.prototype.dateAdd = function(size,value) {
        value = parseInt(value);
        var incr = 0;
        switch (size) {
            case 'day':
                incr = value * 24;
                this.dateAdd('hour',incr);
                break;
            case 'hour':
                incr = value * 60;
                this.dateAdd('minute',incr);
                break;
            case 'week':
                incr = value * 7;
                this.dateAdd('day',incr);
                break;
            case 'minute':
                incr = value * 60;
                this.dateAdd('second',incr);
                break;
            case 'second':
                incr = value * 1000;
                this.dateAdd('millisecond',incr);
                break;
            case 'month':
                value = value + this.getUTCMonth();
                if (value/12>0) {
                    this.dateAdd('year',value/12);
                    value = value % 12;
                }
                this.setUTCMonth(value);
                break;
            case 'millisecond':
                this.setTime(this.getTime() + value);
                break;
            case 'year':
                this.setFullYear(this.getUTCFullYear()+value);
                break;
            default:
                throw new Error('Invalid date increment passed');
                break;
        }
    }
    

    Then just use:

     var d = new Date();
     d.dateAdd('day', -1).dateAdd('year', 3);
    

    T'da

    0 讨论(0)
提交回复
热议问题