469,293 Members | 1,319 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,293 developers. It's quick & easy.

Leap Yr regular expression

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

Thanks

Feb 17 '06 #1
22 4235
VK

deepak.rath...@gmail.com wrote:
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.

Feb 17 '06 #2
de************@gmail.com wrote:
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>
--
Rob
Feb 17 '06 #3
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

Feb 17 '06 #4
RobG wrote:
de************@gmail.com wrote:
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) )

^^
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
Feb 17 '06 #5
JRS: In article <11**********************@o13g2000cwo.googlegroups .com>
, dated Fri, 17 Feb 2006 01:16:15 remote, seen in
news:comp.lang.javascript, de************@gmail.com posted :
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>.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 17 '06 #6
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

Feb 18 '06 #7
VK

de************@gmail.com wrote:
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:

Feb 18 '06 #8
On Sat, 18 Feb 2006 12:49:18 +0200, VK <sc**********@yahoo.com> wrote:
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.

Feb 18 '06 #9
"Vladas Saulis" <vl****@prodata.lt> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Feb 18 '06 #10
On Sat, 18 Feb 2006 19:09:12 +0200, Lasse Reichstein Nielsen
<lr*@hotpop.com> wrote:
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".


Sorry, you mean "!=1" ?

Vladas
Feb 18 '06 #11
Vladas Saulis wrote:
[...] Lasse Reichstein Nielsen [...] wrote:
I would change "== 2) ? 0 : 1" to just "!= 2".


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
Feb 18 '06 #12
On Sat, 18 Feb 2006 19:38:02 +0200, Thomas 'PointedEars' Lahn
<Po*********@web.de> wrote:
Vladas Saulis wrote:
[...] Lasse Reichstein Nielsen [...] wrote:
I would change "== 2) ? 0 : 1" to just "!= 2".


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


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
Feb 18 '06 #13
On Sat, 18 Feb 2006 20:08:19 +0200, Vladas Saulis <vl****@prodata.lt>
wrote:
On Sat, 18 Feb 2006 19:38:02 +0200, Thomas 'PointedEars' Lahn
<Po*********@web.de> wrote:
Vladas Saulis wrote:
[...] Lasse Reichstein Nielsen [...] wrote:
I would change "== 2) ? 0 : 1" to just "!= 2".

Sorry, you mean "!=1" ?


He he, I also missed the point... Sorry. He meant to replace *whole*
expression to "!=2"?
In this I agree.

Vladas
Feb 18 '06 #14
JRS: In article <11**********************@f14g2000cwb.googlegroups .com>
, dated Sat, 18 Feb 2006 02:49:18 remote, seen in
news:comp.lang.javascript, VK <sc**********@yahoo.com> posted :
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 18 '06 #15
JRS: In article <op.s458uhm28lqgld@eightpee>, dated Sat, 18 Feb 2006
15:52:55 remote, seen in news:comp.lang.javascript, Vladas Saulis
<vl****@prodata.lt> posted :
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()

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Feb 18 '06 #16
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

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Feb 19 '06 #17
JRS: In article <ko******************@text.news.blueyonder.co.uk >,
dated Sun, 19 Feb 2006 00:57:52 remote, seen in
news:comp.lang.javascript, Michael Winter <m.******@blueyonder.co.uk>
posted :
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.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demon.co.uk/programs/> - see 00index.htm
Dates - miscdate.htm moredate.htm js-dates.htm pas-time.htm critdate.htm etc.
Feb 19 '06 #18
Dr John Stockton said the following on 2/19/2006 3:04 PM:
JRS: In article <ko******************@text.news.blueyonder.co.uk >,
dated Sun, 19 Feb 2006 00:57:52 remote, seen in
news:comp.lang.javascript, Michael Winter <m.******@blueyonder.co.uk>
posted :
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,


There is nothing "injudicious" about putting VBScript on Web pages.

It's wise if you know when, its ignorant if you don't.
or who more sagaciously use VBscript in WSH :


Sesquipedalian behavior is a sign of ignorance, not intelligence.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 20 '06 #19
On Sun, 19 Feb 2006 02:57:52 +0200, Michael Winter
<m.******@blueyonder.co.uk> wrote:
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
Feb 20 '06 #20
On Sun, 19 Feb 2006 01:25:04 +0200, Dr John Stockton
<jr*@merlyn.demon.co.uk> wrote:
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


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
Feb 20 '06 #21
Vladas Saulis wrote:
[...] Michael Winter [...] wrote:
On 18/02/2006 18:08, Vladas Saulis wrote:
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.


Correct.
Am I right?
You are.
(I have no time to test :).


Typing

javascript:window.alert(new Date(2000, 1, 0));

into your location bar and hit the Return key is too much for you?
PointedEars
Feb 20 '06 #22
JRS: In article <op.s47pc7qy8lqgld@eightpee>, dated Sun, 19 Feb 2006
10:47:21 remote, seen in news:comp.lang.javascript, Vladas Saulis
<vl****@prodata.lt> posted :

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


Use the newsgroup FAQ, and you will learn such things; see below.

With at least one localisation new Date("2005.01.00") gives NaN, as
does new Date("2005-01-00") -- but new Date("2005/01/00")
gives me Fri Dec 31 00:00:00 UTC 2004 and should set the local
equivalent of that anywhere.

One can get St Andrew's Day as new Date((year+1)+"/0/0") .

Also read <URL:http://www.merlyn.demon.co.uk/js-quick.htm>.

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Feb 20 '06 #23

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Buddy | last post: by
4 posts views Thread by Neri | last post: by
11 posts views Thread by Dimitris Georgakopuolos | last post: by
3 posts views Thread by James D. Marshall | last post: by
7 posts views Thread by Billa | last post: by
25 posts views Thread by Mike | last post: by
1 post views Thread by NvrBst | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.