How do I compare dates (earlier, later, difference in days)?

How do I compare dates (earlier, later, difference in days)?

Hi,

I have a form in which I have the users enter two date fields (via the little calendar icon).  I want to compare these two dates and if one is earlier than another, send an email A, otherwise send email B.  The thing is, when I do...:

if (input.date1 < input.date2) { sendemail(); }

...it doesn't quite work in all cases.  For example, if date2 is 12/21/2012 and date1 is 1/10/2013, it'll say that date1 < date2, when actually date2 is earlier than date1.  How do I compare dates and find their relationship relative to one another?

In addition, how do I find the number of dates between two dates?  (e.g. 1/20/2013 and 2/12/2013)

Do the Date fields have built-in functions that allow me to do this?

Thank you for your help!