473,569 Members | 2,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="legalA ge()">
</form>
</HEAD>
<BODY>
<script language="JavaS cript" type="text/JavaScript">

var curdate = new Date()
document.write( curdate.getDate ())
document.write( ".")
document.write( curdate.getMont h() + 1)
document.write( ".")
document.write( curdate.getFull Year())
function legalAge()
{
var ageString;
var legalAge=21;
var ageNum;
ageString = prompt ("Please enter your age", " ");
ageNum = parseInt(ageStr ing);
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 3921
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.javas cript 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******** ************@co mcast.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.javas cript 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(t Date.getFullYea r() - 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.demo n.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(t Date.getFullYea r() - 21);
return (xDate <= tDate);
}
</script>
<form name="theForm" action="">
<label for="bDay">Birt hday (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.getFullYe ar());
</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************ ********@comcas t.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*3 65.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.demo n.co.uk/leapyear.htm#Fr ed>
<URL:http://www.merlyn.demo n.co.uk/js-date0.htm#Age>

--
© 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.
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************ ********@comcas t.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*3 65.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.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #7
JRS: In article <w4************ ********@comcas t.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.demo n.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demo n.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************ ********@comcas t.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.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Jul 23 '05 #9
JRS: In article <Fb************ ********@comcas t.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.demo n.co.uk/> - w. FAQish topics, links, acronyms
PAS EXE etc : <URL:http://www.merlyn.demo n.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
5116
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 for each record, each row accounts for a different type of outcome I'm having trouble with MySQL date comparison. I'm looking for some kind of...
3
3188
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 02 04 and so on.
4
21511
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 (day/month/year) in both case Now my problem is that i want to know if 'datebox' is valid, that means after 'datetoday'.
4
5358
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 calculate days, months, and years. This is not for a college course. It's for my own personal genealogy website. I'm stumped about the code. I'm...
9
5630
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
2592
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
6973
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 = FileDateTime(Application.ExecutablePath) Dim dteTwo As Date = FileDateTime(Application.ExecutablePath) SaveSetting("Test", "Dates", "DateThree",...
12
29435
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 follows: function doDateCheckNow(source, args) { var oDate = document.getElementById(source.controltovalidate); // dd/mm/yyyy
6
5609
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 getTime to compare these two dates. Its working fine for all the cases except if the from date is 31st of a month and to date is the 1st of the following...
0
7612
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7924
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
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...
0
6283
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...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
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...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
937
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...

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.