473,735 Members | 2,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4446
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.demo n.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************ **********@o13g 2000cwo.googleg roups.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.demo n.co.uk/js-valid.htm>.

--
© John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
<URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demo n.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)&&(y ear<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**********@y ahoo.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/rasterTriangleD OM.html>
'Faith without judgement merely degrades the spirit divine.'
Feb 18 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4179
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 regular expressions easier to create and use (and in my experience as a regular expression user, it makes them MUCH easier to create and use.) I'm still working on formal documentation, and in any case, such documentation isn't necessarily the...
4
5157
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 testMyColumn{1, 100}Test Basically I want the regular expression to add the word test infront of the
4
3226
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 over each document, find out if it contains a header and/or a footer and extract only the main content part. The headers and the footers have no specific format and I have to detect and remove them using a list of strings that may appear as...
11
5384
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, I want to use the text included within the brackets to do a lookup so that I can replace the expression with the new text. For example:
3
3218
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 that this will one or more occurrences to replace all the white space between with a comma. This search ElseIf InStr(1, indivline, "$") Then insert a replace statement that uses the regular expression to find and replace all the white space...
7
3828
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 want to avoid that. My question here is if there is a way to pass either a memory stream or array of "find", "replace" expressions or any other way to avoid multiple copies of a string. Any help will be highly appreciated
25
5161
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 (CONDUCTION DEFECT) 37.33/2 HEART (CONDUCTION DEFECT) WITH CATHETER 37.34/2 " the expression is "HEART (CONDUCTION DEFECT)". How do I gain access to the expression (not the matches) at runtime? Thanks, Mike
1
4384
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 the first regular expression that matches the string. I've gor the regular expressions ordered so that the highest priority is first (if two or more regular expressions match the string I want the first one returned) The code that does this has...
1
3407
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 "": --------------------------------- myStringVar = myStringVar.replace("^" + iName + "=,($|&)", ""); --------------------------------- The following DOES replace it though: --------------------------------- var match = myStringVar.match("^" + iName + "=,($|&)");
0
8958
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9193
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8196
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6747
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4555
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4821
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3269
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2186
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.