0

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.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Taywin
0

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
  }
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.