473,402 Members | 2,055 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,402 software developers and data experts.

Date Object problem

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

Jul 5 '06 #1
10 1533
Cylix wrote:
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.

--
Bart

Jul 5 '06 #2
Bart Van der Donck wrote:
Cylix wrote:
>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.
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.
--
Rob
Jul 5 '06 #3
Cylix wrote:
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.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jul 5 '06 #4
RobG <rg***@iinet.net.auwrites:
>Cylix wrote:
>>I have tried below:
var d = new Date(2006, 11, 30); //I expect this return 30 Dec 2006
but it shows "undefined-11-2006"
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
--
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.'
Jul 5 '06 #5
RobG wrote:
[...]
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)

--
Bart

Jul 5 '06 #6
Bart Van der Donck wrote:
RobG wrote:
>[...]
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... :-)
--
Rob
Jul 5 '06 #7
JRS: In article <11**********************@b68g2000cwa.googlegroups .com>
, dated Wed, 5 Jul 2006 10:09:26 remote, seen in
news:comp.lang.javascript, Bart Van der Donck <ba**@nijlen.composted :
>RobG wrote:
>[...]
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.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk DOS 3.3, 6.20; Win98. ©
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links.
PAS EXE TXT ZIP via <URL:http://www.merlyn.demon.co.uk/programs/00index.htm>
My DOS <URL:http://www.merlyn.demon.co.uk/batfiles.htm- also batprogs.htm.
Jul 5 '06 #8
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, RobG <rg***@iinet.net.auposted :
>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 <g>.
>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.
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/&c., FAQqy topics & links;
<URL:http://www.merlyn.demon.co.uk/clpb-faq.txt RAH Prins : c.l.p.b mFAQ;
<URL:ftp://garbo.uwasa.fi/pc/link/tsfaqp.zipTimo Salmi's Turbo Pascal FAQ.
Jul 5 '06 #9
JRS: In article <11*********************@b68g2000cwa.googlegroups. com>,
dated Wed, 5 Jul 2006 02:15:12 remote, seen in
news:comp.lang.javascript, Cylix <cy*******@gmail.composted :
>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.

--
© 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.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Jul 5 '06 #10
var d = new Date('2006/11/30');

Just note that this is not work in safari 1.3.2

Jul 6 '06 #11

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

Similar topics

2
by: amith | last post by:
hi I have written javascript for comparing two dates in US format and finding out whether the start date is greater than the end date and vice versa. In this attempt i have instantiated the...
11
by: Dennis Marks | last post by:
There seems to be a major program with the automatic display of the last modified date. Using the javascript "document.lastModified" sometimes returns the correct date and sometimes 1 Jan 1970...
4
by: Christine | last post by:
I am having the strangest problem. I use the Date function in several of my forms and modules in an Access 2000 mdb. Lately, wherever in my code (in this one mdb) I use the Date function, it...
17
by: ginkim | last post by:
Hi, I'm trying to work with the Australian date formats and javascript and it's causing me quite a headache. I have a date in a "dd/mm/yy" format. Whenever I try to retrieve that date from my...
26
by: jshanman | last post by:
I am writing a timeline that uses Google Maps. I have a function that converts a date time to latitude coords. This function is used to draw the markers on the timeline. I need a reverse function...
5
by: Kermit Piper | last post by:
Hello, I am comparing two date values, one from a database and one that has been converted from a hard-coded string into an actual Date type. So far so good. The problem I'm having is that one...
12
by: Assimalyst | last post by:
Hi, I have a working script that converts a dd/mm/yyyy text box date entry to yyyy/mm/dd and compares it to the current date, giving an error through an asp.net custom validator, it is as...
4
by: Jim Florence | last post by:
Hello, I've just started in ASP and I'm having a few teething problems. Initially I tried to write out dates from the database using <asp:Label runat="server" ID="Label6" Text='<%#...
10
by: Daniel | last post by:
In Microsoft Access I can write a query that includes the criteria: Between Date()-7 And Date() to retrieve the records from a table that fall within the last week. I'm trying to write a...
44
by: user | last post by:
Hi, Let's say I have 2 dates in the b/m format: Date 1 and date 2 How do I check whether Date2 is later than Date 1? Date1. 21-Nov-2006 09:00:00 PM
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.