Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Compare Dates

Reply
Thread Tools

Compare Dates

 
 
Rich
Guest
Posts: n/a
 
      03-28-2005
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>


 
Reply With Quote
 
 
 
 
Randy Webb
Guest
Posts: n/a
 
      03-28-2005
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
 
Reply With Quote
 
 
 
 
Rich
Guest
Posts: n/a
 
      03-28-2005
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" <(E-Mail Removed)> wrote in message
news(E-Mail Removed)...
> 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



 
Reply With Quote
 
Fred Oz
Guest
Posts: n/a
 
      03-28-2005
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
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      03-28-2005
JRS: In article <(E-Mail Removed)>, dated Mon, 28 Mar
2005 00:16:36, seen in news:comp.lang.javascript, Randy Webb
<(E-Mail Removed)> 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.
 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      03-29-2005
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
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      03-29-2005
Dr John Stockton wrote:
> JRS: In article <(E-Mail Removed)>, dated Mon, 28 Mar
> 2005 00:16:36, seen in news:comp.lang.javascript, Randy Webb
> <(E-Mail Removed)> 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
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      03-30-2005
JRS: In article <(E-Mail Removed)>, dated Tue, 29 Mar
2005 17:47:36, seen in news:comp.lang.javascript, Randy Webb
<(E-Mail Removed)> 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.
 
Reply With Quote
 
Randy Webb
Guest
Posts: n/a
 
      04-02-2005
Dr John Stockton wrote:
> JRS: In article <(E-Mail Removed)>, dated Tue, 29 Mar
> 2005 17:47:36, seen in news:comp.lang.javascript, Randy Webb
> <(E-Mail Removed)> 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
 
Reply With Quote
 
Dr John Stockton
Guest
Posts: n/a
 
      04-02-2005
JRS: In article <(E-Mail Removed)>, dated Fri, 1 Apr
2005 19:28:16, seen in news:comp.lang.javascript, Randy Webb
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RFC-822 dates into Ruby dates kellygreer1 Ruby 1 06-08-2008 10:59 PM
Need to use dates earlier than 1900 (Time library says out of range for dates < 1900) me@benjaminarai.com Ruby 1 07-17-2007 02:25 PM
FAQ Dates; Opera Dates. Dr J R Stockton Javascript 0 06-18-2007 03:51 PM
Dates dates dates dates... SQL and ASP.NET David Lozzi ASP .Net 1 09-30-2005 02:18 PM
Dates! Dates! Dates! PW ASP General 4 08-09-2004 04:42 PM



Advertisments