日期对象问题 [英] Date Object problem

查看:62
本文介绍了日期对象问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将通过指定日期来定义日期变量。

我该怎么办?


我在下面尝试过:

var d = new Date(2006,11,30); //我期待2006年12月30日返回

但它显示undefined-11-2006


我发现IE6无法使用:

var d =新日期;

d = d.setFullYear(2006)

d = setMonth(11,30)//或d = setMonth(11) ,这两个陈述都失败了,

如果没有这种方法就会出错。


请指教,谢谢

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

推荐答案

Cylix写道:
Cylix wrote:

我将通过指定日期来定义日期变量。

我该怎么办?


我在下面尝试过:

var d = new Date(2006,11,30); //我期待2006年12月30日返回

但它显示undefined-11-2006


我发现IE6无法使用:

var d =新日期;

d = d.setFullYear(2006)

d = setMonth(11,30)//或d = setMonth(11) ,如果没有这种方法,这两个语句都会失败,

错误。
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.



你不能在javascript中设置时间/日期。你只能读出来。


-

巴特

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

--
Bart


Bart Van der Donck写道:
Bart Van der Donck wrote:

Cylix写道:
Cylix wrote:

>我要定义一个日期变量通过指定日期。
我该怎么办?

我在下面尝试过:
var d = new Date(2006,11,30); //我希望这次回归2006年12月30日
但它显示undefined-11-2006

我发现IE6无法使用:
var d = new Date;
d = d.setFullYear(2006)
d = setMonth(11,30)//或d = setMonth(11),这两个语句都失败了,没有这个方法就会出错。
>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.



你不能在javascript中设置时间/日期。你只能把它读出来。


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



完全垃圾。


请阅读ECMAScript语言规范第15.9节关于

到Date对象 - 如果没有设置日期,setTime,setYear,setMonth,

setDate,setHours,setMinutes等方法是什么?


In关于将Date对象作为构造函数调用并使用特定日期和时间进行初始化,第15.9.3.1节提供了答案:


" new日期(年,月[,日期[,小时[,分钟[,秒[,ms]]]]])


"当使用两到七个参数调用Date时,它从年,月和(可选)日期,小时,分钟,秒计算日期



和ms。


对于OP--诀窍是将参数指定为字符串,而不是数字:


var d =新日期(''2006'',''11'', ''30'';





var d =新日期('20' 06/11/30'');

第二个例子似乎得到了广泛的支持,但可能会有

例外。

-

Rob

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


Cylix写道:
Cylix wrote:

我试过以下:

var d = new Date(2006,11,30); //我希望这个回报2006年12月30日
I have tried below:
var d = new Date(2006, 11, 30); //I expect this return 30 Dec 2006



很好。

Just fine.


但它显示未定义 -11-2006"
but it shows "undefined-11-2006"



什么显示undefined-11-2006?它?什么是它?

如果你只是做

alert(d);

它会正确显示。


我怀疑你的它 (无论是什么向你显示日期)都有问题。


-

Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


这篇关于日期对象问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆