0

I have a requirement to calculate end date given start date and duration. Start date is date and duration is number of years. So end date will be : start date + duration - 1 day.

For e.g. start date is 15/06/2012 and duration is 12 months then end date will be 14/06/2013.

How can we achieve this?

Kijewski
  • 25,517
  • 12
  • 101
  • 143
hetal gala
  • 249
  • 2
  • 5
  • 18

4 Answers4

1

I always create 7 functions, to work with date in JS: addSeconds, addMinutes, addHours, addDays, addWeeks, addMonths, addYears.

You can see an example here: http://jsfiddle.net/tiagoajacobi/YHA8x/

How to use:

var now = new Date();
console.log(now.addWeeks(3));
console.log(now.addYears(1));
console.log(now.addDays(-20));

This are the functions:

Date.prototype.addSeconds = function(seconds) {
    this.setSeconds(this.getSeconds() + seconds);
    return this;
};

Date.prototype.addMinutes = function(minutes) {
    this.setMinutes(this.getMinutes() + minutes);
    return this;
};

Date.prototype.addHours = function(hours) {
    this.setHours(this.getHours() + hours);
    return this;
};

Date.prototype.addDays = function(days) {
    this.setDate(this.getDate() + days);
    return this;
};

Date.prototype.addWeeks = function(weeks) {
    this.addDays(weeks*7);
    return this;
};

Date.prototype.addMonths = function (months) {
    var dt = this.getDate();

    this.setMonth(this.getMonth() + months);
    var currDt = this.getDate();

    if (dt !== currDt) {  
        this.addDays(-currDt);
    }

    return this;
};

Date.prototype.addYears = function(years) {
    var dt = this.getDate();

    this.setFullYear(this.getFullYear() + years);

    var currDt = this.getDate();

    if (dt !== currDt) {  
        this.addDays(-currDt);
    }

    return this;
};
Jacobi
  • 1,508
  • 15
  • 29
0

You cannot do this in jQuery, you have to do it Javascript like so:

<script type="text/javascript">

    // Todays date in milliseconds
    var today=new Date().getTime();

    // Add milliseconds in a year (minus one day) to todays date
    var yearLater=new Date(today + ((31557600 - 86400) * 1000));
    document.write(yearLater);

</script>
SameOldNick
  • 2,397
  • 24
  • 33
0

You don't need jQuery to add date. Javascript is glorious enough to do that.

var myDate=new Date(year,month,date,0,0,0).getTime();
var day_milli= 1000*60*60*24;
var newDate=new Date(myDate + day_milli * (duration -1));
alert(newDate);
Imdad
  • 5,942
  • 4
  • 33
  • 53
  • there won't be any time component in date value.. it is in format dd/mm/yyyy. .even output i need in dd/mm/yyyy format. – hetal gala Jul 18 '12 at 05:46
  • That is why time component is set to `zero`. And you should refer to javascript date functions also http://blog.stevenlevithan.com/archives/date-time-format. You can always get date in your desired format from a date object. http://blog.stevenlevithan.com/archives/date-time-format YOU ARE REALLY LAZY – Imdad Jul 18 '12 at 09:15
  • I tried the way you said ..I get newDate as Sat Aug 18 2012 00:00:00 GMT+0530 (India Standard Time) .. but I am not able to format it as dd-mmm-yyyy(18-Aug-2012) – hetal gala Jul 18 '12 at 13:39
0

An often ignored feature of the Javacript Date object is that setting the individual date attributes (date, month, year, etc) to values beyond their normal range will automatically adjust the other date attributes so that the resulting date is valid.

For example, if d is a date then you can do d.setDate (d.getDate () + 20) and d will be the date 20 days later having adjusted months and even years appropriatley as necessary.

Using this, the following function takes a start date and a duration object and returns the date after the specified time period :

function dateAfter (date, duration) {
  if (typeof duration === 'number') // numeric parameter is number of days
    duration = {date:duration};

  duration.year && date.setYear (date.getYear () + duration.year);
  duration.month && date.setMonth (date.getMonth () + duration.month);
  duration.date && date.setDate (date.getDate () + duration.date);
  return date;
}

// call as follows :

var date = dateAfter (new Date (2012, 5, 15), {year: 1}); // adds one year
console.log (date);

date = dateAfter (date, {month:5, date: 4});  // adds 5 months and 4 days
console.log (date);

date = dateAfter (date, 7 * 4);  // adds 4 weeks
console.log (date);

// to return the end date asked by the op, use 
var date = dateAfter (new Date (2012, 5, 15), {year: 1, date: -1}); 
HBP
  • 15,685
  • 6
  • 28
  • 34