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

Compare Dates

Thanks for the Help in my previous post.

I've been working on this and it's almost what I want.

I want to obtain the user's current age by comparing their date of birth
(user inputs) to the current date.

I know how to get the Current Date but I'm not finding how to calculate the
Current Date minus the User's Birthday.

It would be something like yourage = curdate - bday;

I will then use the results to determine if the User is Over 21 or Under 21.

Any help would be greatly appreciated!

I'm going nuts trying to figure this out.

Is there an Easier Book to learn this stuff other than the "Begining
JavaScript 2nd Edition".

Thanks again for any help. I'm a total novice.

=======================

<HTML>
<HEAD>
<TITLE>Legal Drinking Age </TITLE>
<form>
<input type="button" value="Submit" onClick="legalAge()">
</form>
</HEAD>
<BODY>
<script language="JavaScript" type="text/JavaScript">

var curdate = new Date()
document.write(curdate.getDate())
document.write(".")
document.write(curdate.getMonth() + 1)
document.write(".")
document.write(curdate.getFullYear())
function legalAge()
{
var ageString;
var legalAge=21;
var ageNum;
ageString = prompt ("Please enter your age", " ");
ageNum = parseInt(ageString);
document.write(ageString + "<br>");
if (ageNum >= legalAge)
{alert ("Let's Party!");}
else if (ageNum < legalAge)
{alert ("Sorry! You're too young to drink!");}
else if (ageNum < 0)
{alert ("Please enter a valid age"); }
else
{alert ("Please enter a valid age"); }
}
</SCRIPT>
</BODY>
</HTML>
Jul 23 '05 #1
9 3898
Rich wrote:
Thanks for the Help in my previous post.
Not sure who you are referring to, but, I have to assume you are
referring to me and not to John Stockton. In the future, please quote
what you are replying to and if your new question is a continuation of a
conversation, then don't start a new thread. It makes life simpler on
everyone.
I've been working on this and it's almost what I want.

I want to obtain the user's current age by comparing their date of birth
(user inputs) to the current date.
I know how to get the Current Date but I'm not finding how to calculate the
Current Date minus the User's Birthday.
It would be something like yourage = curdate - bday;
It would be simpler to subtract 21 years from todays date, and then
compare it to the bday.

Create a new Date object with the birthdate parameters, then subtract
and compare.

Its simpler *not* to subtract dates though. Simply make a flow chart of
how you would determine it manually:

Step 1: Subtract years.
Step 2: Is it over 21? Legal Age.
Step 3: Is it under 21? Not Legal.
Step 4: Is it 21? Check the Month.
Step 5: Is the month prior, later or the same as this month?
Step 6: If the month is prior, Legal Age.
Step 7: If the month is later, not Legal.
Step 8: If the month is the same, compare the day of the month.
Step 9: Is the date earlier or the same? Legal Age.
Step 10: Not Legal.

Beware of people trying to make you learn more than you need to know at
this point. Make life as simple as you can and learn as you go.
Is there an Easier Book to learn this stuff other than the "Begining
JavaScript 2nd Edition".


Since I have never seen or read that book, I can't answer that.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #2
I must be tired.

I read what you posted (thank you for the response) but I'm clueles as how
I would make this happen.

How would I compare? In other word what "function, variable or script"
would allow me to do this?

Are there any tutorials online that would help me better understand all of
this?

I understand some stuff but for the most part feel lost with other things.

Thanks again,
"Randy Webb" <Hi************@aol.com> wrote in message
news:xf********************@comcast.com...
Rich wrote:
Thanks for the Help in my previous post.


Not sure who you are referring to, but, I have to assume you are referring
to me and not to John Stockton. In the future, please quote what you are
replying to and if your new question is a continuation of a conversation,
then don't start a new thread. It makes life simpler on everyone.
I've been working on this and it's almost what I want.

I want to obtain the user's current age by comparing their date of birth
(user inputs) to the current date.
I know how to get the Current Date but I'm not finding how to calculate
the Current Date minus the User's Birthday.
It would be something like yourage = curdate - bday;


It would be simpler to subtract 21 years from todays date, and then
compare it to the bday.

Create a new Date object with the birthdate parameters, then subtract and
compare.

Its simpler *not* to subtract dates though. Simply make a flow chart of
how you would determine it manually:

Step 1: Subtract years.
Step 2: Is it over 21? Legal Age.
Step 3: Is it under 21? Not Legal.
Step 4: Is it 21? Check the Month.
Step 5: Is the month prior, later or the same as this month?
Step 6: If the month is prior, Legal Age.
Step 7: If the month is later, not Legal.
Step 8: If the month is the same, compare the day of the month.
Step 9: Is the date earlier or the same? Legal Age.
Step 10: Not Legal.

Beware of people trying to make you learn more than you need to know at
this point. Make life as simple as you can and learn as you go.
Is there an Easier Book to learn this stuff other than the "Begining
JavaScript 2nd Edition".


Since I have never seen or read that book, I can't answer that.

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly

Jul 23 '05 #3
Randy Webb wrote:
Rich wrote: [...]

I want to obtain the user's current age by comparing their date of
birth (user inputs) to the current date.
I know how to get the Current Date but I'm not finding how to
calculate the Current Date minus the User's Birthday.
It would be something like yourage = curdate - bday;

It would be simpler to subtract 21 years from todays date, and then
compare it to the bday.

Create a new Date object with the birthdate parameters, then subtract
and compare.

Its simpler *not* to subtract dates though. Simply make a flow chart of
how you would determine it manually:

Step 1: Subtract years.
Step 2: Is it over 21? Legal Age.
Step 3: Is it under 21? Not Legal.
Step 4: Is it 21? Check the Month.
Step 5: Is the month prior, later or the same as this month?
Step 6: If the month is prior, Legal Age.
Step 7: If the month is later, not Legal.
Step 8: If the month is the same, compare the day of the month.
Step 9: Is the date earlier or the same? Legal Age.
Step 10: Not Legal.


A much simpler algorithm is to subtract 21 years from today's date
and see if the resulting date is bigger than the birth date:

function checkAge(x) { // x = say '21/03/1984'
x = x.split('/');
var xDate = new Date(x[2],x[1]-1,x[0]);
var tDate = new Date();
tDate.setYear(tDate.getFullYear() - 21);
return (xDate <= tDate);
}

Send the birthday in dd/mm/yyyy format and the function returns true
if the date is 21 years or more, otherwise false.

I'm not sure how reliable you need to make this since the user can
enter any date they like, but for the record you may need to allow
for timezones, daylight saving and the vagaries of users' systems
which may have the date set to anything (so perhaps pass your
server's date as the seed for tDate).

Also note that no validation of input is done, it assumes a valid
date has been entered.

Beware of people trying to make you learn more than you need to know at
this point. Make life as simple as you can and learn as you go.


Information can be found at:

<URL:http://www.merlyn.demon.co.uk/js-date1.htm>
[...]
The full script in a page:

<html>
<head>
<title>Over 21</title>
</head>
<body>

<script type="text/javascript">
function checkAge(x) { // x = say '21/03/1984'
x = x.split('/');
var xDate = new Date(x[2],x[1]-1,x[0]);
var tDate = new Date();
tDate.setYear(tDate.getFullYear() - 21);
return (xDate <= tDate);
}
</script>
<form name="theForm" action="">
<label for="bDay">Birthday (dd/mm/yyyy)
<input type="text" id="bDay" value="28/03/1984"></label>
<br>
Your system's current date:
<script type="text/javascript">
var nDate = new Date();
document.write(
nDate.getDate() + '/'
+ (+ nDate.getMonth()+1) + '/'
+ nDate.getFullYear());
</script>
<br>
<input type="button" value="21 or over?" onclick="
alert(checkAge(this.form.bDay.value));
">
<input type="reset">
</form></body>
</html>

--
Fred
Jul 23 '05 #4
JRS: In article <xf********************@comcast.com>, dated Mon, 28 Mar
2005 00:16:36, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Rich wrote:
Thanks for the Help in my previous post.


Not sure who you are referring to, but, I have to assume you are
referring to me and not to John Stockton.


That's not obligatory; but it is permissible.
I know how to get the Current Date but I'm not finding how to calculate the
Current Date minus the User's Birthday.
It would be something like yourage = curdate - bday;


It would be simpler to subtract 21 years from todays date, and then
compare it to the bday.


But not simplest.

The current date is obtained, as milliseconds from 1970-01-01 00:00:00
GMT, in a Date Object by D = new Date. To subtract 21 years it is
necessary to extract the local year, subtract 21, and re-insert (though
the book may say to add 1000*60*60*24*365.25 to the time :-<).

The Birth Date is obtained by asking for (the month, the day, and) the
year; one will be handling that, implicitly or explicitly, as a number
of years, and 21 can very conveniently be added at that stage.
The OP should however consider the case of Frederic of Penzance. If he,
unlike Fred, lives in a country with a written constitution, then
applicable legislation should deal with the point; if not, there should
at least be duly established precedent.
Is there an Easier Book to learn this stuff other than the "Begining
JavaScript 2nd Edition".


Since I have never seen or read that book, I can't answer that.


Never trust a book with a spelling mistake in its own title.

The easiest book is probably in the "Dummies" series, or one of its
competitors. But, rather than looking for a book which will teach
something easily, one should look for a book which will teach something
correctly. The OP should read the newsgroup FAQ.
Refs :
<URL:http://www.merlyn.demon.co.uk/leapyear.htm#Fred>
<URL:http://www.merlyn.demon.co.uk/js-date0.htm#Age>

--
© 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.
Jul 23 '05 #5
Rich wrote:
I must be tired.

I read what you posted (thank you for the response) but I'm clueles as how
I would make this happen.

How would I compare? In other word what "function, variable or script"
would allow me to do this?


Why compare anything? Create a date string for 'today' on your
server. Subtract 21 from the year. Deliver it as text in your page
in an unambiguous format, e.g. "29 March 1984".

Tell users:

"If you were born after 29 March 1984 you are not old enough
to legally consume/purchase/whatever alcoholic beverages
in /insert jurisdiction/ "

or words to that effect.

No client-side JavaScript, no validation, no date arithmetic.
--
Rob
Jul 23 '05 #6
Dr John Stockton wrote:
JRS: In article <xf********************@comcast.com>, dated Mon, 28 Mar
2005 00:16:36, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Rich wrote:

Thanks for the Help in my previous post.
Not sure who you are referring to, but, I have to assume you are
referring to me and not to John Stockton.

That's not obligatory; but it is permissible.


Until someone discovers the secret to eternal life the only thing
obligatory is death. Everything else is optional. But based on the 2
replies, and the helpfulness to a newbe, I made my choice.
I know how to get the Current Date but I'm not finding how to calculate the
Current Date minus the User's Birthday.
It would be something like yourage = curdate - bday;
It would be simpler to subtract 21 years from todays date, and then
compare it to the bday.

But not simplest.


For someone trying to learn the basic concepts of Javascript and Dates,
yes, its simpler, and to me simplest.

The current date is obtained, as milliseconds from 1970-01-01 00:00:00
GMT, in a Date Object by D = new Date. To subtract 21 years it is
necessary to extract the local year, subtract 21, and re-insert (though
the book may say to add 1000*60*60*24*365.25 to the time :-<).
The Birth Date is obtained by asking for (the month, the day, and) the
year; one will be handling that, implicitly or explicitly, as a number
of years, and 21 can very conveniently be added at that stage.
Subtract 21 or add 21, its irrelevant. The principle is the same.

The OP should however consider the case of Frederic of Penzance. If he,
unlike Fred, lives in a country with a written constitution, then
applicable legislation should deal with the point; if not, there should
at least be duly established precedent.


Precedence for what?

--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #7
JRS: In article <w4********************@comcast.com>, dated Tue, 29 Mar
2005 17:47:36, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :

Subtract 21 or add 21, its irrelevant. The principle is the same.


But the practice is different; in this case an instance of the start
year is supplied as a number, but the current year is supplied only
encoded in an Object.
The OP should however consider the case of Frederic of Penzance. If he,
unlike Fred, lives in a country with a written constitution, then
applicable legislation should deal with the point; if not, there should
at least be duly established precedent.


Precedence for what?


How to deal with a situation resembling that of Frederic, of course.
AIUI, his case was fully treated in New York, in 1879, chronologically
in between Paignton & Paris; it was a most ingenious paradox.

--
© 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.
Jul 23 '05 #8
Dr John Stockton wrote:
JRS: In article <w4********************@comcast.com>, dated Tue, 29 Mar
2005 17:47:36, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
Subtract 21 or add 21, its irrelevant. The principle is the same.

But the practice is different; in this case an instance of the start
year is supplied as a number, but the current year is supplied only
encoded in an Object.


Yes, and for a newbe it can be intriguing.

The OP should however consider the case of Frederic of Penzance. If he,
unlike Fred, lives in a country with a written constitution, then
applicable legislation should deal with the point; if not, there should
at least be duly established precedent.


Precedence for what?

How to deal with a situation resembling that of Frederic, of course.
AIUI, his case was fully treated in New York, in 1879, chronologically
in between Paignton & Paris; it was a most ingenious paradox.


Coming from someone who repeatedly says "Do not post URLs when a short
snippet/sample will suffice", that sounds hypocritical of you to rely
upon someone to visit a URL to understand what you are referring to.

As for a birthdate falling on the 29th of February causing problems, it
doesn't to anyone with any intelligence that uses a little common sense,
and does not introduce any paradox. Or, would you prefer to explain the
Paradox?
--
Randy
comp.lang.javascript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #9
JRS: In article <Fb********************@comcast.com>, dated Fri, 1 Apr
2005 19:28:16, seen in news:comp.lang.javascript, Randy Webb
<Hi************@aol.com> posted :
How to deal with a situation resembling that of Frederic, of course.
AIUI, his case was fully treated in New York, in 1879, chronologically
in between Paignton & Paris; it was a most ingenious paradox.
Coming from someone who repeatedly says "Do not post URLs when a short
snippet/sample will suffice", that sounds hypocritical of you to rely
upon someone to visit a URL to understand what you are referring to.

Javascript code is on-topic for this newsgroup; indeed, it is its
purpose. An adequate explanation of Frederic's case is not - hence the
use of the Web reference, for those interested enough to go further.
As for a birthdate falling on the 29th of February causing problems, it
doesn't to anyone with any intelligence that uses a little common sense,
and does not introduce any paradox. Or, would you prefer to explain the
Paradox?


It seems that you have not taken fullest advantage of the URL; that you
have neither followed the link that it contains, nor the traditional
printed version, and do not recall a performance. Sic transit gloria
G&S.


Only a little intelligence and common sense are required, as is also the
case for dealing correctly with the effects of Summer Time. But it is
also necessary to recollect the need to deal with the situations (cf.
Fred), and readers of this newsgroup will surely be aware that such
recollection frequently does not occur.

The OP needs to assure himself that his code will correctly handle a
case such as Fred's, in accordance with applicable legislation; or, at
least, show that he has recognised the potential ambiguity.

--
© 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.
Jul 23 '05 #10

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

Similar topics

4
by: Gleep | last post by:
Hey Guys, I've got a table called Outcomes. With 3 columns and 15 rows 1st col 2nd col 3rdcol outcome date price There are 15 rows...
3
by: jrc4728 | last post by:
I have a MySQL table with the date stored in three fields as string values like this. (sorry, its imported data) str_yy str_dd str_mm ------------------------ 05 01 04 05 ...
4
by: alexis | last post by:
Hi, In a form I have the curent date <input name="datetoday" type="hidden" value="<? echo date("d/m/Y"); ?>"> and <input type=text name="datebox" size=15> The date format is d/m/Y...
4
by: Richard Hollenbeck | last post by:
I'm trying to write some code that will convert any of the most popular standard date formats twice in to something like "dd Mmm yyyy" (i.e. 08 Jan 1908) and compare the first with the second and...
9
by: Rimuen | last post by:
Have two text form with dates i need to compare before submitting, the second should always be a date later the first one. Anyone have a script for this ?? Thanks.......
1
by: godsella | last post by:
First i have two stored procedures, i have passed the values of each one into two different arraylists of dates. how can i compare the two arraylists of dates? Thanks in advance
5
by: Tom | last post by:
It appears that you can't compare two dates in DotNet. You must use ToString and compare the strings. Is that the only reliable way? Try this: Dim dteOne As Date =...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
6
by: cd123 | last post by:
Hi, I used Date object of Javascrips to compare two dates like one is the from date and the other is to date. var obj1 = new Date(from date); var obj2 = new Date(to date); then i used...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.