0

I want to find date in dd/mm/Y format after 60 days from any date submitted by user by date-picker in dd/mm/yy format. I tried searching for it but all i get is 60 days from today's date!!! Pls Help!!!

Genius
  • 65
  • 12
  • Well.. I am pretty new to javascript. Actually I am working with php but i need a javascript code to do this and change automatically a text box's value. So I tried doing some basic things with date() but no result!! – Genius Dec 20 '13 at 10:15

3 Answers3

0

If you want to exclude weekend, the use following:

var startDate = "9-DEC-2011";
startDate = new Date(startDate.replace(/-/g, "/"));
var endDate = "", noOfDaysToAdd = 13, count = 0;
while(count < noOfDaysToAdd){
    endDate = new Date(startDate.setDate(startDate.getDate() + 1));
    if(endDate.getDay() != 0 && endDate.getDay() != 6){
       //Date.getDay() gives weekday starting from 0(Sunday) to 6(Saturday)
       count++;
    }
}
// above code  excludes weekends.
alert(endDate);//You can format this date as per your requirement

You can refer following question for the same :

Add no. of days in a date to get next date(excluding weekends)

If you want to include weekend, the use following:

var date = new Date();
date.setDate(date.getDate() + 32);
// output will be  Tue Jan 21 2014 15:55:56 GMT+0530 (India Standard Time)
Community
  • 1
  • 1
Nishu Tayal
  • 20,106
  • 8
  • 49
  • 101
0

A simpler version using the JavaScript date object:

function toTheFuture(date, days){
  var split = userInput.split("/"),
      //Split up the date and then process it into a Date object
      userDate = new Date(+split[2]+2000, +split[1]-1, +split[0]);

  //Add your days!
  userDate.setDate(userDate.getDate() + days);

  //Return your new dd/mm/yyyy formatted string!
  return addZero(userDate.getDate())+"/"+addZero(userDate.getMonth()+1)+"/"+userDate.getFullYear()
}

//Ensures that if getDate() returns 8 for example, we still get it to output 08
function addZero(num){
  if(num<10){
    num = "0"+num;
  }
  return ""+num;
}

toTheFuture("09/05/13", 60);
//Will return 08/07/2013

The JavaScript date object is slightly more annoying to use than most would like, there are quite a few libraries (e.g. MomentJS) that simplify it to work like you would expect

Doug
  • 3,312
  • 1
  • 24
  • 31
0

There are three distinct subproblems here. First, parse the input in dd/mm/yy format into an object suitable for doing the date arithmetic. Do the +60 days arithmetic. Format the result in dd/mm/Y format.

Javascript has a Date object which is fine for doing the simple arithmetic you need, in my opinion. Adding 60 days to a date is as easy as

givenDate.setDate(givenDate.getDate() + 60)

The setDate method takes care of out of range values, see

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate

For parsing you can extract year, month, and day from your string in the dd/mm/yy format (basically using the string split() method, and feeding the three values to the Date constructor, as in

new Date(year, month, day)

As for formatting, you can use the Date object getter functions to build up your string. See

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Full code:

function parse(dateStr) {
  var parts = dateStr.split('/').map(function (digits) {
    // Beware of leading zeroes...
    return parseInt(digits, 10);
  });
  return new Date(1900 + parts[2], parts[1], parts[0]);
}

function format(date) {
  var parts = [
    date.getDate(),
    date.getMonth(),
    date.getYear() + 1900
  ];
  if (parts[0] < 10) {
    parts[0] = '0' + parts[0];
  }
  if (parts[1] < 10) {
    parts[1] = '0' + parts[1];
  }
  return parts.join('/');
}

function add(date, days) {
  date.setDate(date.getDate() + days);
  return date;
}

console.log(format(add(parse('11/06/83'), 60)));
// Outputs: 09/08/1983
nicolagi
  • 1,289
  • 13
  • 18