473,385 Members | 1,343 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,385 software developers and data experts.

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 4388
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Kenneth McDonald | last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate feedback, suggestions, and criticism as I work towards finalizing the API and feature sets. rex is a module intended to make...
4
by: Buddy | last post by:
Can someone please show me how to create a regular expression to do the following My text is set to MyColumn{1, 100} Test I want a regular expression that sets the text to the following...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
11
by: Dimitris Georgakopuolos | last post by:
Hello, I have a text file that I load up to a string. The text includes certain expression like {firstName} or {userName} that I want to match and then replace with a new expression. However,...
3
by: James D. Marshall | last post by:
The issue at hand, I believe is my comprehension of using regular expression, specially to assist in replacing the expression with other text. using regular expression (\s*) my understanding is...
7
by: Billa | last post by:
Hi, I am replaceing a big string using different regular expressions (see some example at the end of the message). The problem is whenever I apply a "replace" it makes a new copy of string and I...
25
by: Mike | last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in matches. I would like to get what the actual regular expression is. In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
1
by: Allan Ebdrup | last post by:
I have a dynamic list of regular expressions, the expressions don't change very often but they can change. And I have a single string that I want to match the regular expressions against and find...
1
by: NvrBst | last post by:
I want to use the .replace() method with the regular expression /^ %VAR % =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)" regular expression with "":...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.