PDA

View Full Version : problem in using if else condition to compare two dates



meenakshi
02-13-2008, 06:10 PM
hi guys
i m new to this forum and also to javascript
i have a problem to which i m unable to find a solution
pls help me in this regard
i m using a code to change colours of the text and the table and using the following code for that


var color;
var bcolor;
var cdat=(new Date());

if ((rs("ddate"))<cdat)
{color="#4CC417" ; // green colour
bcolor="white" ;}

else if ((rs("ddate"))>cdat)
{color="orange"; // orange colour
bcolor="white";}

else if ((rs("ddate"))==cdat) // red colour
{color="#E41B17";
bcolor="white";}

problem is that the code is calculating < and >dates properly and showing right colours but does not show the right colour when date is equal(==)
it just shows the > colour instead
pls help
thanks
anand

Master_script_maker
02-14-2008, 01:02 AM
it will not be equal because the date() function returns in the following format:
day(three letter abbr.) month(three letter abbr.) day year(all four letters) hour(military time):minute:second GMT-GMT time zone (time zone(i believe only North America))
e.g. Wed Feb 13 2008 20:02:22 GMT-0500 (Eastern Standard Time)

jscheuer1
02-14-2008, 03:45 AM
What's:

rs("ddate")

?

Do you want to compare two dates to the precision of milliseconds, or only to the precision of a day? If a day, is being within 24hrs good enough, or must both dates have the same date number, like the 20th of the month. Should the 20th at 11pm match the 21st at 1am?

meenakshi
02-16-2008, 08:01 PM
hi
i m trying to compare a day not time
that is 20/2/2008 is equal to 20/2/2008
that is all
how should i do that?

jscheuer1
02-16-2008, 09:20 PM
If you have two date objects or one date object and you know the year, month and day to test for:


var cd=new Date(),
cy=cd.getFullYear(),
cm=cd.getMonth(),
cdate=cd.getDate();

This will give you 2008 as cy, cm would be 1 for Feb. (months start at 0 for Jan) and cdate would be 16 (or the current date number).

These can then be compared with similar from another date object or from set values. But the comparison is a little complicated:


<script type="text/javascript">
function when(){
var ty=2008, tm=1, tdate=16;
var cd=new Date(),
cy=cd.getFullYear(),
cm=cd.getMonth(),
cdate=cd.getDate();
if(cy<ty||cy==ty&&cm<tm||cy==ty&&cm==tm&&cdate<tdate)
return 'earlier'
if(cy==ty&&cm==tm&&cdate==tdate)
return 'today'
return 'later';
}
alert(when());
</script>