Results 1 to 5 of 5

Thread: problem in using if else condition to compare two dates

  1. #1
    Join Date
    Nov 2007
    Posts
    69
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default problem in using if else condition to compare two dates

    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

  2. #2
    Join Date
    Jun 2007
    Posts
    543
    Thanks
    3
    Thanked 78 Times in 78 Posts
    Blog Entries
    1

    Default

    it will not be equal because the date() function returns in the following format:
    Code:
    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)
    [Jasme Library (Javascript Motion Effects)] My Site
    /\/\@§†ê® §©®¡þ† /\/\@|{ê®
    There are 10 kinds of people in the world, those that understand binary and those that don't.

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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?
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. #4
    Join Date
    Nov 2007
    Posts
    69
    Thanks
    36
    Thanked 0 Times in 0 Posts

    Default

    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?

  5. #5
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If you have two date objects or one date object and you know the year, month and day to test for:

    Code:
    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:

    Code:
    <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>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. The Following User Says Thank You to jscheuer1 For This Useful Post:

    meenakshi (02-21-2008)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •