Solved: Javascript lying to me?

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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
 

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...
 

tomdkat

Retired Trusted Advisor
Joined
May 6, 2006
Messages
7,148
would this not be more simple if you used a date stamp in php?
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...
 
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
 

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...
 

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...
 
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)
 

tomdkat

Retired Trusted Advisor
Joined
May 6, 2006
Messages
7,148
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)
I saw that too but I figured "addMonths()" was a function the OP created, so these parameters might be correct.

oh and you have missed out a load of ";" i think this might also be playing a part
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...
 

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
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Staff online

Top