Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / JavaScript / November 2004



Tip: Looking for answers? Try searching our database.

Date Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
KL - 27 Nov 2004 23:59 GMT
I am working on a problem and desperately need help!

I need to prompt a user for the numerical month of birth, day of birth and
year of birth and store it in varialbes and the use the variables to build a
date object.  Then print the resulting date object, in it's full form. on
the screen using document.write.  This IS for school and I think I am
starting out right, but can't quite seem to grasp what I am doing wrong, or
where to go from here.  Any tips would be greatly appreciated.

Here is what I have so far...

<script>
var mob=prompt("What is your month of birth, numerically?", "04");
var dob=prompt("What is the date of your birth, numerically?", "07");
var yob=prompt("What is the year of your birth, numerically?", "1967");
month.setMonth(mob-1);
day.setDate(dob);
year.setFullYear(yob);
document.write("Your birthday is " + date + "<br>");
</script>

KL
Lee - 28 Nov 2004 00:48 GMT
KL said:

>I am working on a problem and desperately need help!
>
[quoted text clipped - 16 lines]
>document.write("Your birthday is " + date + "<br>");
></script>

You want to have a single date object, not one each for month, day and year.
The easiest way to do that is to use the form of the Date() constructor that
takes month, day and year as arguments.

var date=new Date(...left for you to find...or ask about...);
document.write(...as you have it...);
KL - 28 Nov 2004 03:37 GMT
> KL said:
>>
[quoted text clipped - 29 lines]
> var date=new Date(...left for you to find...or ask about...);
> document.write(...as you have it...);

OK I have read and reread this and I think I get some of it.  But I am
confused as to what a Date() Constructor is.  I don't think we covered that
in class and I don't find it in my book...or I didn't look right..I will
look again, but in the meantime, what can someone tell me about the Date()
Constructor?

KL
RobG - 28 Nov 2004 04:29 GMT
[...]
> OK I have read and reread this and I think I get some of it.  But I am
> confused as to what a Date() Constructor is.  I don't think we covered that
> in class and I don't find it in my book...or I didn't look right..I will
> look again, but in the meantime, what can someone tell me about the Date()
> Constructor?

 Try the following:

<script type="text/javascript">
  var mob=prompt("What is your month of birth, numerically?","04");
  var dob=prompt("What is the date of your birth, numerically?","07");
  var yob=prompt("What is the year of your birth, numerically?","1967");

  var bDay= new Date(yob,mob,dob);
  alert(bDay);
</script>

Signature

Rob

KL - 28 Nov 2004 05:06 GMT
> [...]
>> OK I have read and reread this and I think I get some of it.  But I am
[quoted text clipped - 13 lines]
>   alert(bDay);
> </script>

Thanks Rob...that helped immensely.  I get what I was doing wrong.  And best
of all it works now :)

KL
Dr John Stockton - 28 Nov 2004 20:52 GMT
JRS:  In article <41a95ba1_1@127.0.0.1>, dated Sat, 27 Nov 2004
23:06:24, seen in news:comp.lang.javascript, KL <klbjornme@aohell.com>
posted :

>> [...]
>>> OK I have read and reread this and I think I get some of it.  But I am
[quoted text clipped - 16 lines]
>Thanks Rob...that helped immensely.  I get what I was doing wrong.  And best
>of all it works now :)

You saw Rob's omission, then?

Moreover, note that by reading back (two of) the fields from bDay, you
can verify the date as being good Gregorian.  Be careful, though, with
historical dates; William Penn's son John "The American", the first by
his second wife, was born on 1700-02-29.  See sig below.

Signature

© 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.

RobG - 29 Nov 2004 00:29 GMT
[..]

> Moreover, note that by reading back (two of) the fields from bDay, you
> can verify the date as being good Gregorian.  Be careful, though, with
> historical dates; William Penn's son John "The American", the first by
> his second wife, was born on 1700-02-29.  See sig below.
>  

 Yes, I forgot that.  It is certainly an issue as different browsers
 support different ranges.

 I think the consensus was that since getFullYear() is not always
 available, checking that the created date object's date and month were
 equal to the input date and month was simplest (provided the OP
 remembers to add one to the month...).

Signature

Rob

KL - 29 Nov 2004 03:46 GMT
> [..]
>>
[quoted text clipped - 10 lines]
>  equal to the input date and month was simplest (provided the OP
>  remembers to add one to the month...).

I remembered about the date offset of 1, but I am lost as to the other
issue.  Can you really dumb it down for me?

KL
RobG - 29 Nov 2004 04:51 GMT
[...]
>> Yes, I forgot that.  It is certainly an issue as different browsers
>> support different ranges.
[quoted text clipped - 6 lines]
> I remembered about the date offset of 1, but I am lost as to the other
> issue.  Can you really dumb it down for me?

 When you create a date object using year, month and day you may be
 trying to create a date outside the range that the browser can handle.
 Since you don't know what browser a user may be using, the easiest way
 is to test the date you create against the input you created it from
 (assuming you've validated that the input was OK in the first place,
 that is that the year is a number, month is between 1 and 12 inclusive
 and the day date between 1 and 31 inclusive).

 Consider the following function:

   function makeDate(y,m,d) {
     var theDate = new Date(y,m-1,d);
     return theDate;
   }

 Provided we call it with something like:

     alert('The date is ' + makeDate('2003','0','5'));

 we should get an alert telling us the date is 5 January, 2003.  In some
 browsers, (e.g. Safari) if we use:

     alert('The date is ' + makeDate('1803','0','5'));

 we'll get an error because it can't handle that dates before 1900 or
 after 2038 (most other browsers can).  So the best way to test is,
 after creating our date, to use:

     if(theDate.getMonth() != m || theDate.getDate() != d) {
       alert('Can't handle date ' + y +'-' + m + '-' + d);
       return false;
     } else {
       return theDate;
     }

 If you want to be really sure, add: || theDate.getFullYear() != y

 However, getFullYear() may not be implemented and rather than messing
 around with testing if it is or not, then seeing if the year is less
 than 1900 then adding 1900, just don't test the year.  The chance that
 the month and date will be OK but year incorrect is pretty slim (in
 this particular case).

 Having said that, if you are going to use getFullYear(), you must test
 that the browser supports it first and if not, use some alternative
 (say use getYear() and if it returns a value less than 1900, add 1900).

 For more fun, follow the link in Dr J's signature:

 <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates,
sources.

Signature

Rob

Dr John Stockton - 29 Nov 2004 13:28 GMT
JRS:  In article <gVxqd.897$I52.38511@news.optus.net.au>, dated Mon, 29
Nov 2004 04:51:24, seen in news:comp.lang.javascript, RobG
<rgqld@iinet.net.auau> posted :

>  When you create a date object using year, month and day you may be
>  trying to create a date outside the range that the browser can handle.
[quoted text clipped - 3 lines]
>  that is that the year is a number, month is between 1 and 12 inclusive
>  and the day date between 1 and 31 inclusive).

I do not believe that the parenthesised validation is needed, at least
in a proper browser.  One can shovel any old rubbish into new Date(Y, M,
D), and it will store NaN unless the stuff converts to a date within
range ( > +-200000 years ); if M or D is outside range, a value of
milliseconds is stored corresponding to YMD.  Date methods return NaN if
the object holds NaN; and NaN is unequal to anything, even another NaN.

The only caveat concerns entering years 00..99, which may be interpreted
as 1900-1999; that makes a difference only for A.D. 0, which for error
rectification was not Leap so it's actually right, but A.D. 4 was
likewise not Leap --- but for dates before 1582-10-15 one must want the
Julian Rules anyway.

>  Having said that, if you are going to use getFullYear(), you must test
>  that the browser supports it first and if not, use some alternative
>  (say use getYear() and if it returns a value less than 1900, add 1900).

Not completely reliable.  I have been assured that in at least one
system the method  getYear  will return 4 for the current year -
recorded in js-date0.htm#gY - can anyone name such browsers?

>  <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates,
>sources.


Signature

© 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.

KL - 29 Nov 2004 13:28 GMT
> [...]
>>> Yes, I forgot that.  It is certainly an issue as different browsers
[quoted text clipped - 59 lines]
>  <URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates,
> sources.

Thanks for that!  I am learning so much!!

KL
Dr John Stockton - 29 Nov 2004 20:09 GMT
JRS:  In article <41ab2304@127.0.0.1>, dated Mon, 29 Nov 2004 07:28:45,
seen in news:comp.lang.javascript, KL <klbjornme@aohell.com> posted :

>Lines: 79

>> ...

>Thanks for that!  I am learning so much!!

Do, though, learn FAQ section 2.3, including the first part of para 6.

Signature

© 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.

Dr John Stockton - 29 Nov 2004 21:05 GMT
JRS:  In article <gVxqd.897$I52.38511@news.optus.net.au>, dated Mon, 29
Nov 2004 04:51:24, seen in news:comp.lang.javascript, RobG
<rgqld@iinet.net.auau> posted :

>In some
>  browsers, (e.g. Safari) if we use:
[quoted text clipped - 3 lines]
>  we'll get an error because it can't handle that dates before 1900 or
>  after 2038 (most other browsers can).

In IE4, the following test code, executed (in my js-quick.htm), shows
(without perceptible delay)

       8639999999999990 Sat Sep 13 00:59:59 UTC+0100 275760
       -8639999999999990 Tue Apr 20 01:00:00 UTC+0100 271822 B.C.

What do other browsers give?  Do any fail?

       B1 = 0 ; B2 = +1e20
       do { T = (B1+B2)/2
         isNaN(new Date(T)) ? B2 = T : B1 = T } while (B2-B1>50)
       document.write(T, ' ', new Date(T))
              document.write('<br>')
       B1 = 0 ; B2 = -1e20
       do { T = (B1+B2)/2
         isNaN(new Date(T)) ? B2 = T : B1 = T } while (B1-B2>50)
       document.write(T, ' ', new Date(T))

The 2038 limitation is presumably to 2038-01-19 Tue 03:14:07 GMT which
is 1970-01-01 + 2^31 - 1 ms.

But 1970-01-01 - 2^31 ms is 1901-12-13 Fri 20:45:52 GMT, which suggests
that your statement may be under-pessimistic.

Given the possible use of script to calculate 30-year mortgages, failure
for 2038 could bite fairly soon.

Signature

© 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.

Fred Oz - 30 Nov 2004 13:07 GMT
[...]
> In IE4, the following test code, executed (in my js-quick.htm), shows
> (without perceptible delay)
[quoted text clipped - 19 lines]
> But 1970-01-01 - 2^31 ms is 1901-12-13 Fri 20:45:52 GMT, which suggests
> that your statement may be under-pessimistic.

 Yes, but I was not concerned about exactitude, more just making the
 point that one should always check that generated dates are correct and
 not just rely on whoever wrote the JS date function for whatever
 browser the user may chose to use.

 However, since you brought it up... the following results are from
 Safari 1.2.3 on Mac OS 10.3.

 Your guestimate is almost correct.  Using the 'js-quick' script gave
 without perceptible delay:

      100000000000000000000 Tue Jan 19 2038 13:14:07 GMT+1000
     -100000000000000000000 Sat Dec 14 1901 06:45:52 GMT+1000

 Manual testing confirmed the limits as:

      1901-12-14 06:45:52  to  2038-01-19 13:14:07

 I think the timezone is irrelvant, I've left it in for comleteness.

 Given that KHTML (on which Safari is based) claims to be ECMA
 compliant, I can only presume this is a quirk of Safari since I have no
 resources to test KHTML.

 The spec requires a range of approximately 285,616 years either side of
 01 January, 1970.  Clearly Safari fails in this case.

 As for other browsers, the following are all on Mac OS X 10.2.8:

 *Camino 0.8.1*  *Firefox 1.0*  *Netscape 7.2*  *Opera 6.03*

     8639999999999990 Sat Sep 13 275760 09:59:59 GMT+1000 (EST)
    -8639999999999990 Tue Apr 20 -271821 10:00:00 GMT+1000 (EST)

 *IE 5.2* varied slightly in format only:

     8639999999999990 Sat Sep 13 09:59:59 UTC+1000 275760
    -8639999999999990 Tue Apr 20 10:00:00 UTC+1000 271822 B.C.

> Given the possible use of script to calculate 30-year mortgages, failure
> for 2038 could bite fairly soon.
>  

 Yes.  I've posted at Apple's support discussions to find an answer.

Signature

Fred

Dr John Stockton - 30 Nov 2004 21:41 GMT
JRS:  In article <41ac70aa$0$25788$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Tue, 30 Nov 2004 23:07:33, seen in
news:comp.lang.javascript, Fred Oz <ozfred@iinet.net.auau> posted :

>  Yes, but I was not concerned about exactitude, more just making the
>  point that one should always check that generated dates are correct and
[quoted text clipped - 15 lines]
>
>  I think the timezone is irrelvant, I've left it in for comleteness.

No; if you choose to live 10 hours ahead of GMT, you should recognise
the consequences !   Those, and those below, are in exact agreement with
what I expected, allowing for the milliseconds of uncertainty in the
method.  Are you a horse rider and anywhere near Glen Innes / Armidale?

>  Given that KHTML (on which Safari is based) claims to be ECMA
>  compliant, I can only presume this is a quirk of Safari since I have no
>  resources to test KHTML.
>
>  The spec requires a range of approximately 285,616 years either side of
>  01 January, 1970.  Clearly Safari fails in this case.

It seems rather likely that they've used a "C" date/time package, using
time_t as a signed 31-bit 1970-based integer.  That might explain why
the big numbers are "exact" above, which is *mildly* surprising; the
Date Object might not support milliseconds.

>  As for other browsers, the following are all on Mac OS X 10.2.8:
>
>  *Camino 0.8.1*  *Firefox 1.0*  *Netscape 7.2*  *Opera 6.03*
>
>      8639999999999990 Sat Sep 13 275760 09:59:59 GMT+1000 (EST)
>     -8639999999999990 Tue Apr 20 -271821 10:00:00 GMT+1000 (EST)

Illustrates the ambiguity of EST.

>  *IE 5.2* varied slightly in format only:
>
>      8639999999999990 Sat Sep 13 09:59:59 UTC+1000 275760
>     -8639999999999990 Tue Apr 20 10:00:00 UTC+1000 271822 B.C.

I've put the code, modified to show GMT/UTC & astronomical notation, in
<URL:http://www.merlyn.demon.co.uk/js-date0.htm#SDB>.  If I hear that it
ever breaks the page, I'll need to change it.

Signature

© 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.

Dr John Stockton - 29 Nov 2004 13:27 GMT
JRS:  In article <41aa9a90@127.0.0.1>, dated Sun, 28 Nov 2004 21:46:32,
seen in news:comp.lang.javascript, KL <klbjornme@aohell.com> posted :
>> [..]
>>>
>>> Moreover, note that by reading back (two of) the fields from bDay, you
>>> can verify the date as being good Gregorian.  Be careful, though, with
>>> historical dates; William Penn's son John "The American", the first by
>>> his second wife, was born on 1700-02-29.  See sig below.

>I remembered about the date offset of 1, but I am lost as to the other
>issue.  Can you really dumb it down for me?

John Penn was born on the Julian Calendar; and so was everyone born in
the British Empire / Colonies before 1752-09-14 Gregorian.  The
particular date that he chose to appear exists in the Julian Calendar,
but not the Gregorian.  The javascript date object code treats the
Gregorian calendar as valid over the whole range.  For Julian calendar
methods, see <URL:http://www.merlyn.demon.co.uk/js-date8.htm#JCDM>.

I seek further interesting events of that date, other than the death of
Samuel Lathrop.

Signature

© 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.

Dr John Stockton - 29 Nov 2004 13:27 GMT
JRS:  In article <04uqd.890$I52.38096@news.optus.net.au>, dated Mon, 29
Nov 2004 00:29:48, seen in news:comp.lang.javascript, RobG
<rgqld@iinet.net.auau> posted :
>[..]
>>
[quoted text clipped - 6 lines]
>  Yes, I forgot that.  It is certainly an issue as different browsers
>  support different ranges.

If so, they should be sorted into byte order and recycled!

The ECMA standard requires a range of 10^8 days, or possibly one less,
from 1970-01-01.

>  I think the consensus was that since getFullYear() is not always
>  available, checking that the created date object's date and month were
>  equal to the input date and month was simplest (provided the OP
>  remembers to add one to the month...).

The following function should return the full year, for all dates and
browsers; but I've not tested it browsers-wide :-

       function getFY(D) { var YE
         YE = Math.round(D.getTime() / 31556952000) + 1970
         return YE + (D.getYear()-YE)%100 }

The principle is that, since the lengths of years do not vary greatly,
YE will always be correct within perhaps a day; and that the use of
getYear() will put the last two digits of the year correct.

Signature

© John Stockton, Surrey, UK.  ?@merlyn.demon.co.uk   Turnpike v4.00   MIME. ©
Web  <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Proper <= 4-line sig. separator as above, a line exactly "-- " (SonOfRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SonOfRFC1036)

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.