Say I have 2 input fields, both containing dates with the format (dd mmm yyyy), for example, 07 Mar 2010 (Yes, there is a space between date, month and year). Well basically I need the second input box to calculate 60 days (not 2 months) after the first text box. Can anyone help? So far this is the code that I have, but it calculates 6 months, not 60 days as required.

Can anyone help?

Thanks in advance.

There are many ways to do it. One way to do it is as followed:

// jsctript
// expect str to be in date format of 'dd mmm yyyy'
function get60DaysAgo(str) {
  var dateArr = str.split(/\s+/)  // break the string at white spaces
  if (dateArr.length==3) {  // simple check, but not complete!
    // create new Date using the string as "May 01, 2010"
    var aDate = Date.parse(dateArr[1]+" "+dateArr[0]+", "+dateArr[2])
    var aTime = aDate.getTime()
    // there are 1000 milliseconds in 1 second
    // there are 60 seconds in 1 minute
    // there are 60 minutes in 1 hour
    // there are 24 hours in 1 day
    // compute the 60 days
    var a60DaysAgo = new Date(aTime - (60*60*60*24*1000))
    alert(a60DaysAgo)  // show it in alert
  }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.