473,395 Members | 1,495 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

A couple of date queries

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

May 13 '06 #1
15 1425
Whoops! Line 3 above should have read:

var dd = now.getDate();

May 13 '06 #2
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;

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 14 '06 #3
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

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

Dave

May 14 '06 #5

"Dave Rado" <da**@rado.4dinternet.co.uk> schreef in bericht
news:11*********************@i40g2000cwc.googlegro ups.com...
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');
}
</script>

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

May 14 '06 #6
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/>

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
May 14 '06 #7
Lee <RE**************@cox.net> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
May 14 '06 #8
Lasse Reichstein Nielsen wrote:
Lee writes:
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)".


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

Richard.
May 14 '06 #9

Lasse Reichstein Nielsen wrote:
Lee <RE**************@cox.net> writes:
"%" 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)".


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

May 14 '06 #10
JRS: In article <11*********************@i40g2000cwc.googlegroups. com>,
dated Sat, 13 May 2006 13:53:15 remote, seen in
news:comp.lang.javascript, Dave Rado <da**@rado.4dinternet.co.uk> posted
:
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.

--
© 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.
May 14 '06 #11
JRS: In article <e4*********@drn.newsguy.com>, dated Sat, 13 May 2006
19:51:37 remote, seen in news:comp.lang.javascript, Lee
<RE**************@cox.net> posted :

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.

--
© 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.
May 14 '06 #12
JRS: In article <11*********************@i40g2000cwc.googlegroups. com>,
dated Sun, 14 May 2006 02:31:47 remote, seen in
news:comp.lang.javascript, Dave Rado <da**@rado.4dinternet.co.uk> posted
:
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.

--
© 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.
May 14 '06 #13
JRS: In article <od**********@hotpop.com>, dated Sun, 14 May 2006
17:55:25 remote, seen in news:comp.lang.javascript, Lasse Reichstein
Nielsen <lr*@hotpop.com> posted :
Lee <RE**************@cox.net> writes:
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)".


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.
May 14 '06 #14
JRS: In article <11**********************@i39g2000cwa.googlegroups .com>
, dated Sun, 14 May 2006 09:50:59 remote, seen in
news:comp.lang.javascript, Dave Rado <da**@rado.4dinternet.co.uk> posted
:

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.

--
© 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.
May 14 '06 #15
Dave Rado wrote:
Lasse Reichstein Nielsen wrote:
Lee <RE**************@cox.net> writes:
> "%" 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)".


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
--
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"
May 22 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: EnriqueM | last post by:
One question: I created an access database that has been extremely useful in my work. I dont' even know at what level of knowledge I am at. I read a lot about how to create a database, created many...
5
by: David B | last post by:
I have a number of queries, running one after the other, which do quite a complex calculation. A text box on a form provides the date for this routine. I have another routine I wish to do and it...
1
by: Ken | last post by:
I wrote a function to use in queries that takes a date and adds or subtracts a certain length time and then returns the new value. There are times when my function needs to return Null values. ...
10
by: Kenneth | last post by:
I have a Query that consist of a lot of different sales data, and one of the colums are different date. The date goes from 1jan2003 til 31jan2003. in this Query I only want the salesdata for...
2
by: carl.barrett | last post by:
Hi, I'm back with the same question as I still can't get it to display my data the way I want it to. The table lists information about a perpetrator involved with an anti social behaviour...
6
by: Tony Miller | last post by:
All I have an aggregate query using the function Month & Year on a datereceived field ie: TheYear: Year() TheMonth: Month() These are the group by fields to give me a Count on another field by...
9
by: mharrison | last post by:
Hello, I am developing a small java web-based car-pool booking system app which interacts with an access database. I am trying to write 2 queries: The first which will specify whether a given car...
19
by: eskelies | last post by:
I have two queries. My goal is to pull a range of numbers that fall on or in a certain date range. For example, $100 on 9/1/2007 and $200 on 9/24/2007. I want the date range to pick up both the...
3
by: murch.alexander | last post by:
I made a simple public function to set and return a date value (see below). I have a number of queries that call up the function to get the "As Of Date," which is typically set to today's date....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.