Click to See Complete Forum and Search --> : Date difference in js


shanuragu
08-26-2003, 01:24 AM
Hi

I have two date fields dt1 & dt2. Any function in js to check whether dt1>=dt2.

shara

Gollum
08-26-2003, 03:09 AM
use the Date object.
You will need to work out how to translate your date fields to Date values - the most reliable way is to get the year,month,day elements out and construct your Dates from that...

var dt1 = new Date(y1,m1,d1);
var dt2 = new Date(y2,m2,d2);

if ( dt1.getTime() >= dt2.getTime() ) alert('dt1 on or after dt2');

Charles
08-26-2003, 05:20 AM
The constructor for the Date object can be used a couple of different ways. If passed just a string it will parse that string, understanding several different date formats. And you don't need to create new variables.

alert(new Date('4 July 2003') < new Date('26/8/2003'))

See http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/date.html for details.

shanuragu
08-28-2003, 11:12 PM
var book_fdate = document.form.book_dt_from.value;
var book_tdate = document.form.book_dt_to.value;
if (book_fdate.getDate()>=book_tdate.getDate())
{
alert("Error in Date Field");
return false;
}

what is wrong with this code???

shara:confused:

Charles
08-29-2003, 06:49 AM
Originally posted by shanuragu
what is wrong with this code???Form element values are strings. getDate() is a property of the Date object. There is no String.getDate(). Try instead:

if (new Date(document.form.book_dt_from.value).getDate() >= new Date(document.form.book_dt_to.value).getDate()) {}

shanuragu
09-02-2003, 12:50 AM
No, it is not working..

here
new Date(document.form.book_dt_from.value).getDate()
& new Date(document.form.book_dt_from.value)
value is "NaN"??? hence no validation is done.
The date in book_dt_form & book_dt_to is dd/mmm/yyyy format.
How can I solve this? Should I check for date & month separetly???


shara

Charles
09-02-2003, 07:31 AM
Working with dates can be a little problematic. Strictly numerical date formats are ambiguous. Is 5/9/2003 the ninth of May or the fifth of September? The Date object constructor will understand several formats, but there's no telling how it will handle an ambiguous date. It should use which ever convention is common to the locale of the user. But you never know. (You also need to keep in mind that the dawn of time didn't happen until 1 January 1970.)

One solution to the ambiguity problem is to be a tyrant about it and enforce just one date format. If you go that route then you will have to split the date into its parts and use the constructor as Gollum has demonstrated above.

The solution that I prefer is to allow users to input the Date however they like and to use the "onchange" handler to immediately transform the date into an unambiguous format. Below I'm using the native "toDateString()" method which uses the format associated with the user's locale, but that can be overwritten.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="Content-Script-Type" content="text/javascript">
<title>Example</title>
<style type="text/css">
<!--
label {display:block; margin:1em 0em}
input {display:block}
-->
</style>
<script type="text/javascript">
<!--
function validate (f) {
if (new Date(f.end.value).getTime() <= new Date(f.end.value).getTime()) {alert('Start needs to come before end.'); return false}
}
// -->
</script>
<form action="" onsubmit="return validate(this)">
<div>
<label>Start<input type="text" name="start" onchange="this.value = new Date(this.value).toDateString()"></label>
<label>End<input type="text" name="end" onchange="this.value = new Date(this.value).toDateString()"></label>
<button type="submit">Submit</button>
</div>
</form>

shanuragu
09-03-2003, 07:43 AM
Thanks for ur reply. After going through ur suggestions I have desided to separate date field in to date, month, year & validate. There is no other option. Because user is taking from_dt & to_dt from a calender which will be in dd/mmm/yyyy format (that is the format we are currently using throughout the project & is accepted by SQL server).

Good suggestions/ help on this topic is most wel come.

shara:;)