Click to See Complete Forum and Search --> : Date compare in js
shanuragu
09-05-2003, 12:51 AM
Hi
How to compare whether from_date >= to_date using js.
From_date & to_date are text field values & always will be in
the format 12/Aug/2003. Since text field date value will be a string, I am unable to do the date compare.
please help.
shara
Gollum
09-05-2003, 02:07 AM
you could try replacing the "/" with " " in the strings from your text field and constructing a Date object from it:
var dtString = document.theForm.dateField.value;
var dt = new Date(dtString.replace(/[/]/g," "));
then you can use dt.getTime() to get the millisecs since 1 January 1970 00:00:00 and use that to compare.
shanuragu
09-05-2003, 03:00 AM
I am getting syntax error at the second line ???
ie,var fdtString = document.form.book_dt_from.value;
var dt = new Date(fdtString.replace(-[-]-g," "));
shara
Gollum
09-05-2003, 03:38 AM
Hmmm, are you now formatting dates as dd-mmm-yyyy?
because you have changed all the "/" chars in the regular expression in the replace function with "-" chars. The first and last "/" are necessary for Javascript to recognise it as a regular expression.
Try
var dt = new Date(fdtString.replace(/[-]/g," "));
shanuragu
09-05-2003, 04:24 AM
Thanks a lot ..
Shara
shanuragu
09-05-2003, 06:46 AM
Any function in js to compare dates?? If not how can we compare whole date format??
shara
Charles
09-05-2003, 06:54 AM
In JavaScript all Ojects have two built in methods that are called as needed, valueOf() and toString(). The Date object's toString() method returns the date in that strange format and its valueOf() method returns the number of miliseconds since 1 January 1970, the dawn of time. If you use any Object as a string then its toString() method is called automatically.
<script type="text/javascript">
<!--
alert(new Date());
// -->
</script>
But if you try to use an Object as a number then its valueOf() method is called.
<script type="text/javascript">
<!--
alert(new Date() > new Date('4 July 2003'));
// -->
</script>
shanuragu
09-05-2003, 07:44 AM
thanks :D
any web site where in I can get detailed info about js functions & methods???
shara
Gollum
09-05-2003, 07:54 AM
someone here posted this one some time ago...
http://devedge.netscape.com/library/manuals/2000/javascript/1.5/reference/