Date Object problem

C

Cylix

I am going to define a date variable by specify a Date.
How can I do so?

I have tried below:
var d = new Date(2006, 11, 30); //I expect this return 30 Dec 2006
but it shows "undefined-11-2006"

and I found IE6 cannot using:
var d=new Date;
d=d.setFullYear(2006)
d=setMonth(11,30) // or d=setMonth(11), both of these statements fail,
error on without this method.

Please advise, Thanks
 
B

Bart Van der Donck

Cylix said:
I am going to define a date variable by specify a Date.
How can I do so?

I have tried below:
var d = new Date(2006, 11, 30); //I expect this return 30 Dec 2006
but it shows "undefined-11-2006"

and I found IE6 cannot using:
var d=new Date;
d=d.setFullYear(2006)
d=setMonth(11,30) // or d=setMonth(11), both of these statements fail,
error on without this method.

You can't set the time/date in javascript. You can only read it out.
 
R

RobG

Bart said:
You can't set the time/date in javascript. You can only read it out.

Complete rubbish.

Please read the ECMAScript Language Specification section 15.9 in regard
to the Date object - what are methods setTime, setYear, setMonth,
setDate, setHours, setMinutes, etc. for if not to set the date?

In regard to calling the Date object as a constructor and intitialising
it with a particular date and time, section 15.9.3.1 provides the answer:

"new Date (year, month [, date[ ,hours[, minutes[, seconds[ ,ms]]]]] )

"When Date is called with two to seven arguments, it computes the date
from year, month, and (optionally) date, hours, minutes, seconds
and ms."

For the OP - the trick is to specify the parameters as strings, not numbers:

var d = new Date('2006', '11', '30');

or

var d = new Date('2006/11/30');


The second example seems pretty widely supported but there may be
exceptions.
 
M

Matt Kruse

Cylix said:
I have tried below:
var d = new Date(2006, 11, 30); //I expect this return 30 Dec 2006

Just fine.
but it shows "undefined-11-2006"

What shows "undefined-11-2006"? It? What is "it"?
If you just do
alert(d);
it will show correctly.

I suspect your "it" (whatever is showing you the date) has a problem in it.
 
L

Lasse Reichstein Nielsen

Surprising. It should work as expected. When I do it, it prints:
"Sat, 30 Dec 2006 00:00:00 GMT+0100".

In regard to calling the Date object as a constructor and
intitialising it with a particular date and time, section 15.9.3.1
provides the answer:

"new Date (year, month [, date[ ,hours[, minutes[, seconds[ ,ms]]]]] )

"When Date is called with two to seven arguments, it computes the date
from year, month, and (optionally) date, hours, minutes, seconds
and ms."

For the OP - the trick is to specify the parameters as strings, not numbers:

var d = new Date('2006', '11', '30');

Why? The first thing the Date constructor does is to parse the strings
back into numbers.
var d = new Date('2006/11/30');

That can work too, but the specification does not guarantee which
formats are supported.
The second example seems pretty widely supported but there may be
exceptions.

Indeed.

/L
 
B

Bart Van der Donck

RobG said:
[...]
Complete rubbish.
[...]

It seems I misinterpreted the OP's question, but my statement still
stands:

'You can't set the time/date in javascript. You can only read it out.'
(meaning: adjusting the system clock)
 
R

RobG

Bart said:
RobG said:
[...]
Complete rubbish.
[...]

It seems I misinterpreted the OP's question, but my statement still
stands:

'You can't set the time/date in javascript. You can only read it out.'
(meaning: adjusting the system clock)

In my best Darth Vader voice:

Apology accepted, Captain Bart... :)
 
D

Dr John Stockton

JRS: In article <[email protected]>
, dated Wed, 5 Jul 2006 10:09:26 remote, seen in
news:comp.lang.javascript said:
RobG said:
[...]
Complete rubbish.
[...]

It seems I misinterpreted the OP's question, but my statement still
stands:

'You can't set the time/date in javascript. You can only read it out.'
(meaning: adjusting the system clock)

Probably not entirely true.

It should be impossible in javascript on a Web page, but it may be
possible with JScript (a form of javascript) executing under WSH, and
maybe in an HTA.
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Wed, 5 Jul 2006 02:15:12 remote, seen in
news:comp.lang.javascript said:
I am going to define a date variable by specify a Date.
How can I do so?

I have tried below:
var d = new Date(2006, 11, 30); //I expect this return 30 Dec 2006

It will certainly not do that. It will generate a Date Object whose
..valueOf() (which tends to show the true internal form of things) is the
number of milliseconds from 1970-01-01 00:00:00 GMT to the beginning of
2006-12-30 in your computer's local time; your computer should be
correctly set for locality (including Summer time, for those in the
Southern Hemisphere).
but it shows "undefined-11-2006"

That's an unspecified "it"; the routine you use to provide the date part
is evidently grossly defective, and that for the month part may be OK if
you add 1. Unseen code is generally wrong and cannot be corrected here.

and I found IE6 cannot using:
var d=new Date;
That should be new Date(), but new Date may do as well.
BUT it sets the current local date/time ...
d=d.setFullYear(2006)
If that date/time does not exist in 2006, an unexpected change will
occur. Some code using new Date() can perhaps only be fully verified on
February 29th.
d=setMonth(11,30) // or d=setMonth(11), both of these statements fail,
You've omitted for each the d. which you have with setFullYear().
error on without this method.

Read the newsgroup FAQ; see sig below; never attempt to set a date
piecemeal; never call new Date() unless you want the *current*
value, since new Date(0) gives a consistent result several times
faster. And check what you write, both in coding and here in News.

Post complete "working" examples, saying what you think it should do,
stating the result that is hoped for, and what the result is.
 
D

Dr John Stockton

JRS: In article <44ab8f3e$0$14119$5a62ac22@per-qv1-newsreader-
01.iinet.net.au>, dated Wed, 5 Jul 2006 20:06:00 remote, seen in
news:comp.lang.javascript said:
Complete rubbish.
For the OP - the trick is to specify the parameters as strings, not numbers:

var d = new Date('2006', '11', '30');

as above said:
or

var d = new Date('2006/11/30');


The second example seems pretty widely supported but there may be
exceptions.

The first gives December, the second November.

It is not guaranteed, or even suggested IIRC, in the ECMA standard; such
formats are left to the implementor. This may be because ECMA could not
in practice rule out the FFF stupidity in most extant browsers nor rule
in the acceptable of the ISO date separator, but could not bring itself
to approve them either.

But I've frequently asserted, in a "tone" inviting refutation, that
new Date("YYYY/MM/DD") always works; often enough, with no refutation,
that I think it can be taken as reliable.
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top