-2

I'm trying to compare two dates for exactly one month. I used the code below for the validation. Its working for this case:

from Date:1/5/2013 to Date:1/6/2013

but not working if we consider February month, e.g.:

fromDate:28/2/2013; toDate:31/3/2013;

Can you let me know the solution?

I tried the code below (source), but it's not working.

var fromDate = new Date(document.getElementById("Billing_From").value);
var toDate = new Date(document.getElementById("Billing_To").value);
fromDate.setMonth( fromDate.getMonth() + 1 );
if((fromDate-toDate) !=0)
{
    alert("Please limit the date range to 1 month.");
}
Community
  • 1
  • 1
RajVish
  • 191
  • 2
  • 13
  • What's the correct behaviour you want? Are you saying it should be - for example - from 28 Feb to 28 March? But from 31 March to 30 April (because there isn't a 31 April)? – boisvert Jun 03 '13 at 11:16
  • You can try downloading datejs: http://www.datejs.com/ and then use the `isAfter` or `isBefore` methods to check your date range. – Tallmaris Jun 03 '13 at 11:31

1 Answers1

0

Keep the following in mind.

  1. The default format is MM/dd/yyyy.
  2. In Javascript Date months are from 0(Jan) to 11(Dec).

This code is for comparing same dates of adjacent months. Example - 15th Feb, 2013 and 15th March, 2013.

What is you exact definition of month ?

sarveshseri
  • 13,738
  • 28
  • 47