Leap Yr regular expression

D

deepak.rathore

can someone give the regular expr. to validate leap yr like
02/29/2000,02/29/00

Thanks
 
V

VK

can someone give the regular expr. to validate leap yr like
02/29/2000,02/29/00

As soon as you tell us the rule you want to use for two-digits years
like "00" or "01" etc.

Should it be 0 A.D., 1900 A.D., 2000 A.D. or even 3000 A.D. (if you're
making a Futurama calendar widget :)

Windows, for instance, interprets by default two-digits year as year
between 1930 and 2029. It is not an international standard of any kind,
just a sample of choice.
 
R

RobG

can someone give the regular expr. to validate leap yr like
02/29/2000,02/29/00

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) )
{
// Year is a leap year
} else {
// Year is not a leap year
}

or

if ( (year%4 == 0 && year%100 > 0) || year%400 == 0)
{
// year is a leap year
}


If your intention is to validate a date, there are two basic methods:

1. Use a series of logic tests to check the date vs days in
the month and whether or not it is a leap year, or

2. Use the components of the date to create a date object,
then check if the year and month of the constructed date
object are the same as those input.

Search the archives for date validation routines based on the above.
There are other methods, but they are less obvious. Try:

<URL:http://www.merlyn.demon.co.uk/js-date4.htm>
 
D

deepak.rathore

I want a regular expression for this. Not any C program.
00-99 would mean 1950 to 2049 ( i hope this is what in sybase)
Four digit any number 0000 -9999
Any ptrs as what r the sequences for such number.

Thanks
 
T

Thomas 'PointedEars' Lahn

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) )
^^
Forcing type conversion to boolean here is unnecessarily inefficient.
If the result of this expression is evaluated and it is not boolean,
it is automatically type-converted to boolean anyway.


PointedEars
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Fri, 17 Feb 2006 01:16:15 remote, seen in
can someone give the regular expr. to validate leap yr like
02/29/2000,02/29/00

Usenet is an international medium, though Google might prefer you to
think otherwise. Therefore, numeric dates should be given is ISO 8601
format, and certainly not in FFF.

The only sensible reason for determining whether a year is a leap year
using just a regular expression is as a pedagogical exercise aimed at
giving practice at the use of regular expressions. If you are given an
answer, you will not learn anything.

You might learn something about RegExps from
<URL:http://www.merlyn.demon.co.uk/js-valid.htm>.
 
D

deepak.rathore

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.

-Cheers
 
V

VK

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.

I don't actually understand why are you calling it "pattern" or
"regular expression". Regular expressions handle combinations of
symbols to search/replace them.

isLeapYear problem is a *mathematical* one (can be divided to 4 w/o
reminder or not in the most promitive form).

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. But in the
holly name why? 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;
}
else if ((year>=50)&&(year<1000)){
year+=1900;
}
else {
/*NOP*/
}

// 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:
 
V

Vladas Saulis

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:

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.
Anyway, I still use it in my pages, and it was never failed.

Vladas
ProData Ltd.

P.S. Just entered this newsgroup. It is very interesting.
 
L

Lasse Reichstein Nielsen

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

/L
 
V

Vladas Saulis

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

Sorry, you mean "!=1" ?

Vladas
 
T

Thomas 'PointedEars' Lahn

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
 
V

Vladas Saulis

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

I think you all didn't understand the point:

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

Vladas
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Sat, 18 Feb 2006 02:49:18 remote, seen in
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.

That's actually trivial to express, and the expression must be rather
like

JulianLeap = /([02468][048]|[13579][26])$/.test(Year)

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

Your presumed country, under the benevolent rule of His Majesty George
II, upgraded from that rule in 1752. Luke, ch10, v37, tail.
 
D

Dr John Stockton

JRS: In article <op.s458uhm28lqgld@eightpee>, dated Sat, 18 Feb 2006
15:52:55 remote, seen in Vladas Saulis
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);
}

A function of that nature should return a Boolean :

return new Date(year, 1, 29, 0, 0).getMonth() != 2

The following is preferable, though the programmer can do the addition :

return !new Date(year, 0, 366+31).getMonth()
 
M

Michael Winter

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.

Mike
 
V

Vladas Saulis

A function of that nature should return a Boolean :

return new Date(year, 1, 29, 0, 0).getMonth() != 2

Originally it was a function returning the number of days in February,
i.e.:

return ((new Date(year, 1, 29, 0, 0).getMonth() == 2) ? 28 : 29);

I need nothing more than that in my systems. And I just corrected this
function to the needs
of this topic. Of course, it must have been improved.

Vladas
 
V

Vladas Saulis

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.

Following this logic, then the date = YYYY.MM.00 should return the last
day of the previous month.
Am I right? (I have no time to test :).

Vladas
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Sun, 19 Feb 2006 00:57:52 remote, seen in
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.

For those who injudiciously put VBscript on Web pages, or who more
sagaciously use VBscript in WSH :

Functions DateSerial and TimeSerial have corresponding behaviour.
 

Ask a Question

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.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top