Connecting Tech Pros Worldwide Help | Site Map

A couple of date queries

Dave Rado
Guest
 
Posts: n/a
#1: May 13 '06
1) I want to be able to display the date in dd format.

If in my script file I have:

var dd = DayOfMonthArray[now.getDate()];

and in my html page I have:

<script type="text/javascript">
document.write(dd);
</script>

Then the date displays in d fomat, so instead of 01 I get 1.


But if in my script file I have:

var DayOfMonthArray = new Array
("01","02","03","04","05","06","07","08",
"09","10","11","12","13","14","15","16","17","18", "19","20","21","22",
"23","24","25","26","27","28","29","30","31");
var dd = DayOfMonthArray[now.getDate()];


and in my html page I have:

<script type="text/javascript">
document.write(dd);
</script>

Then it adds 1 to the date!! So instead of 01 I get 02! Why? And what's
the best way of getting round it?


2) I want to be able to display the year in yy format. now.getYear()
displays the yeat in yyyy format; what is the best way to get it to
display in yy format?

Dave

Dave Rado
Guest
 
Posts: n/a
#2: May 13 '06

re: A couple of date queries


Whoops! Line 3 above should have read:

var dd = now.getDate();

Evertjan.
Guest
 
Posts: n/a
#3: May 14 '06

re: A couple of date queries


Dave Rado wrote on 13 mei 2006 in comp.lang.javascript:
[color=blue]
> var DayOfMonthArray = new Array
> ("01","02","03","04","05","06","07","08",
> "09","10","11","12","13","14","15","16","17","18", "19","20","21","
> 22", "23","24","25","26","27","28","29","30","31");
> var dd = DayOfMonthArray[now.getDate()];
>[/color]

The first element of an array has number 0,
so on the 1st the result is 02
and
on the 31st the result wil be undefined.

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

But wy use an array?

Try for the above:

var dd = new Date();
dd = dd.getDate();
dd = (dd<10)?'0'+dd:''+dd;

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Dave Rado
Guest
 
Posts: n/a
#4: May 14 '06

re: A couple of date queries


Thanks, Lee.

1) When I use your code in my html page, it works. But when I put your
code into my script file (with just the "document.wite" statement in my
html file), I get an "Object not found" error. Any idea what I could
have done wrong? In my script file I have:

function zeroPad(n) {
return (n>10)?n:"0"+n;
}

var now = new Date()
var yy=zeropad(now.getFullYear()%100);

If instead I have in my scipt file:

var yy=now.getFullYear()%100;
var yy = (yy<10)?'0'+yy:''+yy

... then it works! I.e. it doesn't seem to like the function when it's
in the script file, although it's happy with the function when it's on
the html page instead! Weird.

2) What does the %100 bit mean?

3) Given what you said about getYear() returning an unreliable number
of digits, how can I return a guaranteed four-digit year?


Dave

Dave Rado
Guest
 
Posts: n/a
#5: May 14 '06

re: A couple of date queries


Thanks, that works well, although I'd like to use Lee's function if I
could get it to work.

Dave

Marc
Guest
 
Posts: n/a
#6: May 14 '06

re: A couple of date queries



"Dave Rado" <dave@rado.4dinternet.co.uk> schreef in bericht
news:1147599107.365598.76740@i40g2000cwc.googlegro ups.com...[color=blue]
> Thanks, Lee.
>
> 1) When I use your code in my html page, it works. But when I put your
> code into my script file (with just the "document.wite" statement in my
> html file), I get an "Object not found" error. Any idea what I could
> have done wrong? In my script file I have:
>
> function zeroPad(n) {
> return (n>10)?n:"0"+n;
> }
>[/color]

That's probably happening because the page isn't fully loaded yet. Put Lee's
function back in the script file and document write on page loading
(completed) like the following:


<script type="text/javascript">
window.onload = function(){
document.write('the stuff here');
}
</script>

[color=blue]
> var now = new Date()
> var yy=zeropad(now.getFullYear()%100);
>
> If instead I have in my scipt file:
>
> var yy=now.getFullYear()%100;
> var yy = (yy<10)?'0'+yy:''+yy
>
> .. then it works! I.e. it doesn't seem to like the function when it's
> in the script file, although it's happy with the function when it's on
> the html page instead! Weird.
>
> 2) What does the %100 bit mean?
>
> 3) Given what you said about getYear() returning an unreliable number
> of digits, how can I return a guaranteed four-digit year?
>
>
> Dave
>[/color]


Evertjan.
Guest
 
Posts: n/a
#7: May 14 '06

re: A couple of date queries


Dave Rado wrote on 14 mei 2006 in comp.lang.javascript:
[color=blue]
> Thanks, that works well, although I'd like to use Lee's function if I
> could get it to work.[/color]

What works wel?

Please quote what you are replying to.

If you want to post a followup via groups.google.com, don't use the
"Reply" link at the bottom of the article. Click on "show options" at the
top of the article, then click on the "Reply" at the bottom of the article
headers. <http://www.safalra.com/special/googlegroupsreply/>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#8: May 14 '06

re: A couple of date queries


Lee <REM0VElbspamtrap@cox.net> writes:
[color=blue]
> http://docs.sun.com/source/816-6408-10/ops.htm#1042403
>
> "%" is the modulus operator. It returns the remainder
> of the division operation. x%100 evaluates to the two
> right-most digits of x.[/color]

That's a slightly misleading description of "x%100". The result
is a number, not a string, so a more correct description
would be "evalutates to the value of the two right-most digits
of x (or the digit, if there is only one)".

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Richard Cornford
Guest
 
Posts: n/a
#9: May 14 '06

re: A couple of date queries


Lasse Reichstein Nielsen wrote:[color=blue]
> Lee writes:[color=green]
>> http://docs.sun.com/source/816-6408-10/ops.htm#1042403
>>
>> "%" is the modulus operator. It returns the remainder
>> of the division operation. x%100 evaluates to the two
>> right-most digits of x.[/color]
>
> That's a slightly misleading description of "x%100". The
> result is a number, not a string, so a more correct
> description would be "evalutates to the value of the two
> right-most digits of x (or the digit, if there is only one)".[/color]

And a slightly more correct version may cover the outcome when x is not
an integer. ;-)

Richard.


Dave Rado
Guest
 
Posts: n/a
#10: May 14 '06

re: A couple of date queries



Lasse Reichstein Nielsen wrote:
[color=blue]
> Lee <REM0VElbspamtrap@cox.net> writes:[color=green]
> > "%" is the modulus operator. It returns the remainder
> > of the division operation. x%100 evaluates to the two
> > right-most digits of x.[/color]
>
> That's a slightly misleading description of "x%100". The result
> is a number, not a string, so a more correct description
> would be "evalutates to the value of the two right-most digits
> of x (or the digit, if there is only one)".[/color]

Odd that the description refers to a division operation, when in this
instance - now.getFullYear()%100) - there is no division operation!

Anyway, it's working now. Thanks everyone.

Dave

Dr John Stockton
Guest
 
Posts: n/a
#11: May 14 '06

re: A couple of date queries


JRS: In article <1147553595.204158.40710@i40g2000cwc.googlegroups. com>,
dated Sat, 13 May 2006 13:53:15 remote, seen in
news:comp.lang.javascript, Dave Rado <dave@rado.4dinternet.co.uk> posted
:[color=blue]
>1) I want to be able to display the date in dd format.[/color]

But we know what the date is.

function LZ(x) { return (x>=10||x<0?"":"0") + x }

D = now.getDate()
document.writeln("The Date is ", LZ(D), " today.")
[color=blue]
> ...[/color]

[color=blue]
>But if in my script file I have:
>
>var DayOfMonthArray = new Array
>("01","02","03","04","05","06","07","08",
> "09","10","11","12","13","14","15","16","17","18", "19","20","21","22",
> "23","24","25","26","27","28","29","30","31");
>var dd = DayOfMonthArray[now.getDate()];
>
>
>and in my html page I have:
>
><script type="text/javascript">
> document.write(dd);
></script>
>
>Then it adds 1 to the date!![/color]

No, it does not add 1 to anything; it only gives you a string
representing a number one larger than you wanted.
[color=blue]
>So instead of 01 I get 02! Why? And what's
>the best way of getting round it?[/color]

The cause is that arrays start at element 0, not 1. You *could* use an
initial comma (or "00",)

var DayOfMonthArray = new Array
(, "01","02","03","04", ...
or
var DayOfMonthArray = [, "01","02","03","04", ...

Actually, "000102030405...3031".substr(now.getDate()*2, 2) should do;
but use LZ.

[color=blue]
>2) I want to be able to display the year in yy format. now.getYear()
>displays the yeat in yyyy format;[/color]

It does not. It returns an integer, the last two decimal digits of
which are the last two digits of the year, and the first two may
currently be zero(?), 1, or 20, depending on the browser.

One must be aware of the type of quantity that a variable may have at
each stage, especially String/Number.
[color=blue]
> what is the best way to get it to
>display in yy format?[/color]

..., LZ(now.getYear()%100), ...

Or
now.getFullYear().toString().substring(2)

but that will fail after about 7993.5 years.

Read the newsgroup FAQ; see below.

--
© 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
Guest
 
Posts: n/a
#12: May 14 '06

re: A couple of date queries


JRS: In article <e465vp01b1j@drn.newsguy.com>, dated Sat, 13 May 2006
19:51:37 remote, seen in news:comp.lang.javascript, Lee
<REM0VElbspamtrap@cox.net> posted :[color=blue]
>
>The getYear() method returns 2, 3 or 4 digits, depending on
>the browser and the year in question.[/color]

Not so.

It returns a Number, an Object holding an IEEE Double. That number
modulo 100 equals the actual year represented modulo 100 . For the
years 1999,2000 it may return 99,0 or 99,100 or 99,2001, depending on
browser.

ISTM important to be aware of the types of variables and function &
method results.

--
© 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
Guest
 
Posts: n/a
#13: May 14 '06

re: A couple of date queries


JRS: In article <1147599107.365598.76740@i40g2000cwc.googlegroups. com>,
dated Sun, 14 May 2006 02:31:47 remote, seen in
news:comp.lang.javascript, Dave Rado <dave@rado.4dinternet.co.uk> posted
:
[color=blue]
>1) When I use your code in my html page, it works. But when I put your
>code into my script file (with just the "document.wite" statement in my[/color]
wite ?[color=blue]
>html file), I get an "Object not found" error. Any idea what I could
>have done wrong? In my script file I have:
>
>function zeroPad(n) {
> return (n>10)?n:"0"+n;
>}
>
>var now = new Date()
>var yy=zeropad(now.getFullYear()%100);
>
>If instead I have in my scipt file:[/color]
scipt ?[color=blue]
>
>var yy=now.getFullYear()%100;
>var yy = (yy<10)?'0'+yy:''+yy
>
>.. then it works![/color]

Well, clearly you don't type reliably. Javascript is case-sensitive,
and so zeropad & zeroPad are entirely different - and in your
case only one exists.
[color=blue]
>3) Given what you said about getYear() returning an unreliable number
>of digits, how can I return a guaranteed four-digit year?[/color]

(a) He's wrong; it returns a Number.
(b) With getFullYear, in browsers not before about MSIE4 level
(c) With getFY, in js-date0.htm, via FAQ & sig.

--
© 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
Guest
 
Posts: n/a
#14: May 14 '06

re: A couple of date queries


JRS: In article <ody01tzm.fsf@hotpop.com>, dated Sun, 14 May 2006
17:55:25 remote, seen in news:comp.lang.javascript, Lasse Reichstein
Nielsen <lrn@hotpop.com> posted :[color=blue]
>Lee <REM0VElbspamtrap@cox.net> writes:
>[color=green]
>> http://docs.sun.com/source/816-6408-10/ops.htm#1042403
>>
>> "%" is the modulus operator. It returns the remainder
>> of the division operation. x%100 evaluates to the two
>> right-most digits of x.[/color]
>
>That's a slightly misleading description of "x%100". The result
>is a number, not a string, so a more correct description
>would be "evalutates to the value of the two right-most digits
>of x (or the digit, if there is only one)".[/color]

Well, "decimal" should be there somewhere ... "to the value of all
digits not to the left of the second digit before the position of the
decimal point"?

But I'd prefer a purely arithmetic explanation independent of the value
of the second operand, as in ECMA-262 11.5.3; though I'd first present a
version for non-negative integer operands, such as "Generally, X%Y is
the remainder on dividing the non-negative integer X by the positive
integer Y; in particular languages the implementation is extended to
negative and/or negative operands in more than one way."


--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
Dr John Stockton
Guest
 
Posts: n/a
#15: May 14 '06

re: A couple of date queries


JRS: In article <1147625459.219019.258030@i39g2000cwa.googlegroups .com>
, dated Sun, 14 May 2006 09:50:59 remote, seen in
news:comp.lang.javascript, Dave Rado <dave@rado.4dinternet.co.uk> posted
:[color=blue]
>
>Odd that the description refers to a division operation, when in this
>instance - now.getFullYear()%100) - there is no division operation![/color]

The value of getFullYear is _divided_ into two parts; the first, which
is the largest multiple of 100 that fits in Year, is dropped, and the
result is the remainder.

In practice, % will be implemented by a hardware division.

--
© 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.
Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#16: May 22 '06

re: A couple of date queries


Dave Rado wrote:
[color=blue]
> Lasse Reichstein Nielsen wrote:[color=green]
>> Lee <REM0VElbspamtrap@cox.net> writes:[color=darkred]
>> > "%" is the modulus operator. It returns the remainder
>> > of the division operation. x%100 evaluates to the two
>> > right-most digits of x.[/color]
>>
>> That's a slightly misleading description of "x%100". The result
>> is a number, not a string, so a more correct description
>> would be "evalutates to the value of the two right-most digits
>> of x (or the digit, if there is only one)".[/color]
>
> Odd that the description refers to a division operation, when in this
> instance - now.getFullYear()%100) - there is no division operation![/color]

Yes, there is. Internally. Or do you think the machine simply /guesses/
the remainder? :)


PointedEars
--
But he had not that supreme gift of the artist, the knowledge of
when to stop.
-- Sherlock Holmes in Sir Arthur Conan Doyle's
"The Adventure of the Norwood Builder"
Closed Thread