D
deepak.rathore
can someone give the regular expr. to validate leap yr like
02/29/2000,02/29/00
Thanks
02/29/2000,02/29/00
Thanks
[email protected] said:can someone give the regular expr. to validate leap yr like
02/29/2000,02/29/00
can someone give the regular expr. to validate leap yr like
02/29/2000,02/29/00
^^RobG said:A year is a leap year if it is evenly divisible by four and not 100, or
if evenly divisible by 400. It can be written as:
if ( (!(year%4) && !!(year%100)) || !(year%400) )
can someone give the regular expr. to validate leap yr like
02/29/2000,02/29/00
I am just asking for the pattern that is observed in such yrs. I think
this is a public forum, whose purpose is to help others. Any ptrs would
be appreciated.
Although i can get such reg. exp. such from google, i would like to
inderstand the pattern and write one of my own.
isLeapYear problem is a *mathematical* one (can be divided to 4 w/o
reminder or not in the most promitive form).
[skip]
.... Normal mathematical operations will make it much easier
and reliable. Like:
var yr = document.forms['myForm'].elements['myYear'].value;
var year = parseInt(yr,10);
if (year < 50) {
year+= 2000;
}
[skip] ...
// now check that year is divisible by four and return true or false
// also welcome to add any amount extra checks for the correct input:
Vladas Saulis said:It's curious, but I found this working with ALL known (for me) browsers:
function isLeapYear(year) {
return ((new Date(year, 1, 29, 0, 0).getMonth() == 2) ? 0 : 1);
}
Sure, this example exploits a JS implementation, but it seems to be
similar between the browsers.
No exploit, it is behaving as expected, and should work in all compliant
implementations (except for years 0-99 where the year might be normalized
to 1900-1999).
I would change "== 2) ? 0 : 1" to just "!= 2".
Vladas said:Sorry, you mean "!=1" ?
No, that would not be equivalent.
`(x == 2) ? 0 : 1' yields 0 (false) if x equals 2, 1 (true) otherwise.
`x != 1' yields true if x equals 2, false otherwise.
He means
`x != 2' which yields false if x equals 2, true otherwise.
PointedEars
Vladas said:[...] Lasse Reichstein Nielsen [...] wrote:
I would change "== 2) ? 0 : 1" to just "!= 2".
Sorry, you mean "!=1" ?
news:comp.lang.javascript said:It is possible to imagine (but I have no idea if it indeed exists) that
by studying "1900", "1901" etc. *string sequences* one can discover a
pattern for strings representing a number divisible by four.
// now check that year is divisible by four and return true or false
// also welcome to add any amount extra checks for the correct input:
It's curious, but I found this working with ALL known (for me) browsers:
function isLeapYear(year) {
return ((new Date(year, 1, 29, 0, 0).getMonth() == 2) ? 0 : 1);
}
When you create a date = YYYY.02.29 for a non-leap year, you get back
a YYYY.03.01. And the getMonth() for the "March" is equal to 2 (for
february = 1).
That's why I called it kinda an exploit (unexpected (for me)
behaviour).
A function of that nature should return a Boolean :
return new Date(year, 1, 29, 0, 0).getMonth() != 2
On 18/02/2006 18:08, Vladas Saulis wrote:
[snip]
When you create a date = YYYY.02.29 for a non-leap year, you get back
a YYYY.03.01. And the getMonth() for the "March" is equal to 2 (for
february = 1).
That's why I called it kinda an exploit (unexpected (for me)
behaviour).
However, it /is/ expected behaviour. The Date object is well-known for
handling out-of-range values by adjusting adjacent fields. It does this
by design.
news:comp.lang.javascript said:On 18/02/2006 18:08, Vladas Saulis wrote:
[snip]
When you create a date = YYYY.02.29 for a non-leap year, you get back
a YYYY.03.01. And the getMonth() for the "March" is equal to 2 (for
february = 1).
That's why I called it kinda an exploit (unexpected (for me)
behaviour).
However, it /is/ expected behaviour. The Date object is well-known for
handling out-of-range values by adjusting adjacent fields. It does this
by design.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.