A couple of date queries

D

Dave Rado

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
 
L

Lee

Dave Rado said:
Whoops! Line 3 above should have read:

var dd = now.getDate();

I don't see any line numbers. I don't see anything above.
If you have a correction to post, just post the corrected code.
You should be copying your actual code and pasting it into
your newsreader, to avoid accidents.

The code:
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()];

will always assign to dd the date of the following day (except on the 31st,
when you won't get anything. Javascript arrays are indexed beginning at
zero, not one.

You could fix your code by adding "00" at the beginning. It would
never be used, but it would make "01" appear at index position 1.

On the other hand, you could just use a simple function, instead:

function zeroPad(n) {
return (n>10)?n:"0"+n;
}
var dd=zeroPad(now.getDate());

You could use the same funtion with months, being aware that
the getMonth() method returns a value from 0-11:

var mm=zeroPad(1+now.getMonth());

The getYear() method returns 2, 3 or 4 digits, depending on
the browser and the year in question.
It's supposed to return the year with 1900 subtracted from it,
but some browsers have decided that it's better to give the
users what they expect than what the documentation says they
should expect.

var yy=zeroPad(now.getFullYear()%100);

will always be the two digits you want.


--
 
E

Evertjan.

Dave Rado wrote on 13 mei 2006 in comp.lang.javascript:
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()];

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;
 
D

Dave Rado

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
 
D

Dave Rado

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

Dave
 
M

Marc

Dave Rado said:
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;
}

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');
}
 
L

Lee

Dave Rado said:
.. 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.

As Marc said, you're using it before it's loaded.

2) What does the %100 bit mean?

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.

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

http://docs.sun.com/source/816-6408-10/date.htm#1193607

getFullYear() returns the four digit year.


--
 
E

Evertjan.

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

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/>
 
L

Lasse Reichstein Nielsen

Lee said:
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.

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
 
R

Richard Cornford

Lasse said:
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)".

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

Richard.
 
D

Dave Rado

Lasse said:
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)".

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
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Sat, 13 May 2006 13:53:15 remote, seen in
news:comp.lang.javascript said:
1) I want to be able to display the date in dd format.

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.")
...

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!!

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

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.

2) I want to be able to display the year in yy format. now.getYear()
displays the yeat in yyyy format;

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.
what is the best way to get it to
display in yy format?

..., 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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sat, 13 May 2006
19:51:37 remote, seen in Lee
The getYear() method returns 2, 3 or 4 digits, depending on
the browser and the year in question.

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

Dr John Stockton

JRS: In article <[email protected]>,
dated Sun, 14 May 2006 02:31:47 remote, seen in
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 wite ?
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: scipt ?

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

.. then it works!

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.
3) Given what you said about getYear() returning an unreliable number
of digits, how can I return a guaranteed four-digit year?

(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.
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated Sun, 14 May 2006
17:55:25 remote, seen in Lasse Reichstein
Nielsen said:
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)".

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."
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Sun, 14 May 2006 09:50:59 remote, seen in
news:comp.lang.javascript said:
Odd that the description refers to a division operation, when in this
instance - now.getFullYear()%100) - there is no division operation!

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

Thomas 'PointedEars' Lahn

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

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


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top