1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Solved: Javascript lying to me?

Discussion in 'Web Design & Development' started by Dariune, Apr 1, 2010.

Thread Status:
Not open for further replies.
Advertisement
  1. Dariune

    Dariune Thread Starter

    Joined:
    Aug 4, 2005
    Messages:
    101
    Ok maybe its not and instead i have got it wrong but i have a problem which has been puzzling me for a little while and im hoping one of you fine people might be able to shed some light on it.

    I am trying to compare two dates in javascript.
    The first date is one inputted in a dynamic form and the second is todays date.

    I am comparing to see if the 1st date is more than 6 months before todays date.

    The problem i am getting is that despite alerting the test coming up as true it then doesnt show the alert (or vice versa.)

    The code in its current state is ...

    Code:
    for(x=1; x<10; x++){
    						if (document.getElementById("date_"+x).value != "") {
    
    							var currentTime = new Date();
    							var newDate = addMonths(currentTime, -6)
    							var month = (newDate.getMonth() + 1)
    							if (month < 10) {
    								month = "0" + month
    							};
    							var day = newDate.getDate()
    							if (day < 10) {
    								day = "0" + day
    							};
    							var year = newDate.getFullYear()
    							var formatDate = day + "/" + month + "/" + year
    							var robDate = document.getElementById("date_"+x).value
    							var test = (parseInt(document.getElementById("date_"+x).value) < parseInt(formatDate));
    							alert(document.getElementById("date_"+x).value)
    							alert(formatDate)
    							alert(test);
    							if (test == true){
    								alert("Expenses longer than 6 months old cannot be claimed. Please re-enter the date.");
    								return(false);
    							};
    I have also tried if(parseInt(document.getElementById("date_"+x).value) < parseInt(formatDate)); etc and i still get the wrong test despite when alerting it it shows correctly.

    Anyone able to help?

    Dariune
     
  2. tomdkat

    tomdkat Retired Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,148
    Did you post the entire for loop above? Also, in which browser are you testing?

    Peace...
     
  3. Grob needs help

    Grob needs help

    Joined:
    Mar 3, 2009
    Messages:
    327
    would this not be more simple if you used a date stamp in php?
     
  4. tomdkat

    tomdkat Retired Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,148
    I don't follow. They need to take today's date (which is already in the code above), subtract 6 mos from it (with adjustments for the year), and then compare it with a date a user enters into a form field.

    How does a "date stamp" come into play?

    Peace...
     
  5. Grob needs help

    Grob needs help

    Joined:
    Mar 3, 2009
    Messages:
    327
    you could use something like this

    say your form is like this
    <form method="get" action="example.php">
    enter date:<input type="text" name="time" /><input type="submit" value="submit" />
    </form>
    PHP:
    if(isset($_GET[time])){ 
    $unix_time $_GET[time];
    }
    $timestamp time($unix_time);
       if (
    $timestamp 180*24*60*60){
       echo 
    "you can not claim your expenses back!";
       }else{
       echo 
    "you can make a claim";
       }
    where you just change the echo's to say what you want
     
  6. tomdkat

    tomdkat Retired Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,148
    Ok, I'm not sure how that relates to the date entered by the user.....

    Peace...
     
  7. Grob needs help

    Grob needs help

    Joined:
    Mar 3, 2009
    Messages:
    327
    sorry i entered it a bit wrong it should make more sense now
     
  8. tomdkat

    tomdkat Retired Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,148
    Gotcha. I do like that approach but the time() function doesn't appear to accept any arguments and we're dealing with dates. So, "today" would need to be converted into seconds (which is easy) along with the date entered by the user (will need some work) and then we're set.

    Based on the code in the first post of this thread, I'm guessing the date is in a "DD/MM/YYYY" format, so that would need to be ripped apart and converted into seconds (relative to Unix Epoch).

    Getting back to the OPs original question, the main issue is why the if test of "test" isn't working as expected even though the alert() indicates the correct boolean value.

    Peace...
     
  9. Grob needs help

    Grob needs help

    Joined:
    Mar 3, 2009
    Messages:
    327
    i think i may have found your problem
    Code:
    var newDate = addMonths(currentTime, -6)
    
    are you trying to subtract by 6 the surely it should be
    Code:
    var newDate = addMonths(currentTime - 6)
    
     
  10. Grob needs help

    Grob needs help

    Joined:
    Mar 3, 2009
    Messages:
    327
    oh and you have missed out a load of ";" i think this might also be playing a part
     
  11. tomdkat

    tomdkat Retired Trusted Advisor

    Joined:
    May 6, 2006
    Messages:
    7,148
    I saw that too but I figured "addMonths()" was a function the OP created, so these parameters might be correct.

    I noticed that too, which is why I asked which browser(s) are being used for testing. I played around with the code above in Chromium and it didn't like the missing semi-colons but didn't display errors like Firefox does in its Error Console.

    Peace...
     
  12. Grob needs help

    Grob needs help

    Joined:
    Mar 3, 2009
    Messages:
    327
    well we will only know when the op comes back and looks at our suggestions
     
  13. Dariune

    Dariune Thread Starter

    Joined:
    Aug 4, 2005
    Messages:
    101
    Hi all

    Sorry for my late reply. i was having a break from my pc over easter.
    Im back at work now and before replying i thought i would go through some of the things you said and all i can say is thankyou to you all.

    It was infact this ...

    Code:
    var newDate = addMonths(currentTime, -6)
    
    ... that was the problem. My only excuse for coming here with such a stupid problem is that i am very new to the coding game.
    Since my studies this is the first proper application that i have built :)

    I didnt do anything in php because im afraid i dont know php. I only know javascript and ASP (And learning ASP.NET)

    Thanks very much again for all the replies. As ever you have saved me days (literally) of looking at code and scratching my head.

    Dariune
    P.s. TomKat, your right, i forgot to show the addMonths function which didnt help lol
     
  14. Grob needs help

    Grob needs help

    Joined:
    Mar 3, 2009
    Messages:
    327
    well i am glad we could help
     
  15. Dariune

    Dariune Thread Starter

    Joined:
    Aug 4, 2005
    Messages:
    101
    me too :)
     
  16. Sponsor

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Loading...
Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/913977

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice