473,386 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

determine leap year, javascript

hi, i need a javascript that determines if a year is a leap year or not, by putting the year in a text field and hitting a submit button.

i'm fairly new to all this, so would appriciate the help, ta.
Jul 25 '07 #1
12 2199
gits
5,390 Expert Mod 4TB
hi ...

have a look at the following thread:

http://www.thescripts.com/forum/thread663618.html

kind regards
Jul 25 '07 #2
hi ...

have a look at the following thread:

http://www.thescripts.com/forum/thread663618.html

kind regards
thanks gits, i'm using your code in that example, which works grand.

can you tell me what the % sign does? year % 100

also, what does flag mean?

also, in the form submit button, can you explain this code,

onclick="leapYear(document.getElementById('year'). value);"
Jul 25 '07 #3
gits
5,390 Expert Mod 4TB
hi ...

it's not my code ;) ... only helped to get it to work ... 3 things to mention:

1. % is the modulus-operator - have a look here

2. flag is a simple variable in that script ;) doesn't mean anything ... the poster there called it that way ...

3. consider to use the solution that jsakalos provided there ... it's shorter probably better then the other solution

kind regards
Jul 25 '07 #4
hi ...

it's not my code ;) ... only helped to get it to work ... 3 things to mention:

1. % is the modulus-operator - have a look here

2. flag is a simple variable in that script ;) doesn't mean anything ... the poster there called it that way ...

3. consider to use the solution that jsakalos provided there ... it's shorter probably better then the other solution

kind regards
i think i'd better get my head around a basic way, before i use jsakalos's version.

i have to do this in php.

here's what i understand, the user has to enter a year, this number has to be disivable by 4 with no remainders, if the year ends in a 00 it's not a leap year, unless it's every 400 years, then it is.

here's my own code so far,

i'm parsing this through a form.....

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4. $leap = x % 100;
  5.  
  6.  
  7.  
  8. $x = $_POST["txtRequest"]; 
  9.  
  10. if ($leap == 0)
  11.  
  12. {
  13.         echo("this is a leap year");
  14. }
  15.  
  16.  
  17. else 
  18.  
  19.         echo("this is not a leap year");
  20. }
  21.  
  22. ?>
  23.  
  24.  

at the moment, i'm only dividing the number by 4, and seeing if the remainder is 0, this should work right? but it doesn't.

i need to get that working, and then add 2 other variables?

also, why doesn't flay have a var before it, if it is a variable?
Jul 25 '07 #5
ok, i was sure this would work........ but i'm just getting an error, am i close with this one, or am i way off?



Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4. $leap = $x;
  5.  
  6.  
  7.  
  8. $x = $_POST["year"]; 
  9.  
  10. if ($leap == (($x%100) == 0 || ($x%400) == 0  || ($x%4) == 0)
  11.  
  12. {
  13.         echo("this is a leap year");
  14. }
  15.  
  16.  
  17. else 
  18.  
  19.         echo("this is not a leap year");
  20. }
  21.  
  22. ?>
Jul 25 '07 #6
epots9
1,351 Expert 1GB
ok, i was sure this would work........ but i'm just getting an error, am i close with this one, or am i way off?



Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4. $leap = $x;
  5.  
  6.  
  7.  
  8. $x = $_POST["year"]; 
  9.  
  10. if ($leap == (($x%100) == 0 || ($x%400) == 0  || ($x%4) == 0)
  11.  
  12. {
  13.         echo("this is a leap year");
  14. }
  15.  
  16.  
  17. else 
  18.  
  19.         echo("this is not a leap year");
  20. }
  21.  
  22. ?>
what does the error say?

and i thought u wanted to do this with javascript...thats php?
Jul 25 '07 #7
what does the error say?

and i thought u wanted to do this with javascript...thats php?
i have to do it in javascript and php.......... ok here's my latest attempt, in php........


Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4. $leap = $x;
  5.  
  6.  
  7.  
  8. $x = $_POST["year"];
  9.  
  10. if ($leap == (($x%100) == 0 || ($x%400) == 0 || ($x%4) == 0)
  11.  
  12. {
  13. echo("this is a leap year");
  14. }
  15.  
  16.  
  17. else
  18. {
  19.  
  20. echo("this is not a leap year");
  21. }
  22.  
  23. ?>

i'm not getting any errors, but basicly every year is a leap year :/

can you comment on this code, tell me if i'm anywhere close, if i'm on the right track....... i know there's probably a cleaner faster way to do this, but i'm just trying to understand conditionals.

basicly, i'm trying to say in the code, if x is divided by 4, 100 or 400, and you get a module with nothing left over, it's a leap year.

am i making any sence?
Jul 25 '07 #8
epots9
1,351 Expert 1GB
i have to do it in javascript and php.......... ok here's my latest attempt, in php........


Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.  
  4. $leap = $x;
  5.  
  6.  
  7.  
  8. $x = $_POST["year"];
  9.  
  10. if ($leap == (($x%100) == 0 || ($x%400) == 0 || ($x%4) == 0)
  11.  
  12. {
  13. echo("this is a leap year");
  14. }
  15.  
  16.  
  17. else
  18. {
  19.  
  20. echo("this is not a leap year");
  21. }
  22.  
  23. ?>

i'm not getting any errors, but basicly every year is a leap year :/

can you comment on this code, tell me if i'm anywhere close, if i'm on the right track....... i know there's probably a cleaner faster way to do this, but i'm just trying to understand conditionals.

basicly, i'm trying to say in the code, if x is divided by 4, 100 or 400, and you get a module with nothing left over, it's a leap year.

am i making any sence?
this is it:
[php]
$x = $_POST["year"];

if (($x%4) == 0)
{
echo("this is a leap year");
}
else
{
echo("this is not a leap year");
}
[/php]

there is nothing other that that.
Jul 25 '07 #9
this is it:
[php]
$x = $_POST["year"];

if (($x%4) == 0)
{
echo("this is a leap year");
}
else
{
echo("this is not a leap year");
}
[/php]

there is nothing other that that.


ok, a little help from a guy in work, and here it is!
Expand|Select|Wrap|Line Numbers
  1.  
  2. <?php
  3.  
  4. $x = $_POST["year"];
  5.  
  6.  
  7. $leap = $x;
  8.  
  9.  
  10. if ( ( ($x % 4 == 0) && ($x % 100 !=0) )  ||  ($x % 400 == 0) )
  11.  
  12. {
  13.         echo("this is a leap year");
  14. }
  15.  
  16.  
  17. else 
  18.  
  19.         echo("this is not a leap year");
  20. }
  21.  
  22. ?>

my brain's still a bit fuzzy over the whole leap year thing, but i'm starting to get the whole conditional thing, oh, and i know how modules work!
Jul 25 '07 #10
epots9
1,351 Expert 1GB
inside your if, y don't u just use '%4'? y do u need '%100' or '%400'?

if it returns 0 its a leap year, if it returns anything else its not a leap year.
Jul 25 '07 #11
inside your if, y don't u just use '%4'? y do u need '%100' or '%400'?

if it returns 0 its a leap year, if it returns anything else its not a leap year.

ye, you need the other 2, as years ending in 00 is not a leap year, except for every 400 years.
Jul 25 '07 #12
mrhoo
428 256MB
If a year is evenly divisibly by 100 it is not a leap year, unless it is also evenly divisibly by 400. 1900 was not a leap year, 2000 was.
Jul 25 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Dr John Stockton | last post by:
I'm starting a new thread, for clarity. I'm assuming that it's ECMA-262 3rd Edn (1999) that we should be using for Web pages; that 4th Edn (what's its present status? is it available?) is too...
22
by: deepak.rathore | last post by:
can someone give the regular expr. to validate leap yr like 02/29/2000,02/29/00 Thanks
4
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow...
2
by: owz | last post by:
Ok, I am making a program (java class file) 2 work out if a date entered is valid or invalid for the day, month year, and for leap years.. dd/mm/yyyy . I seem 2 have gotten it 2 validate the year...
8
by: rn216_ccc | last post by:
Hi there all, I found a web site,. http://www.onlineconversion.com/leapyear.htm, where it can give a list of leap years by within the given range by the user, or the user may enter a year and the...
3
by: xGx | last post by:
Hi please could someone help. i have created this programming so you can work out if there is a LEAP YEAR. But i need it to handle centuries. heres the code def isLeapYear (year): result =...
2
by: cybermom | last post by:
Hi another newbie here I am trying to get the message: The you entered is a leap year, but it does not happen, what did I do wrong with this script? <html> <HEAD> <SCRIPT...
37
by: mazwolfe | last post by:
I'm new here, so excuse me if my style is incorrect. Can anyone come up with a better method for this calculation? Code: int is_leap(int year) { switch (year % 19) { case 0: case 3: case 6:...
4
by: jrw133 | last post by:
Hello. So im a little bit stuck on one of my homework questions. heres the question: Write a C Shell Script that performs the following functions. Asks the user to input a year. The script...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.