当前位置: 首页 > 网络学院 > 客户端脚本教程 > JavaScript > JS Date

JavaScript
JS 字符串
JS Date
JS数组,JS Array
JS Boolean
JS Math
JS HTML DOM
JS Browser
JS Cookies
JS 校验
JS Animation
JS Image Maps
JS Timing
JS 建立对象
JS 摘要
JS 实例
JS 对象实例
JS DOM 实例
JS数组对象参考
JS布尔对象参考
JS日期对象参考

JavaScript 中的 JS Date


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-01   浏览: 719 ::
收藏到网摘: n/a

The Date object is used to work with dates and times.
时间对象用来操作日期和时间。


Examples
举例

Return today's date and time
How to use the Date() method to get today's date.
使用Date()方法得到今天的日期

getTime()
Use getTime() to calculate the years since 1970.
用getTime()来计算1970年到现在之间的时间差距

setFullYear()
How to use setFullYear() to set a specific date.
使用getFullYear()来设置指定的日期

toUTCString()
How to use toUTCString() to convert today's date (according to UTC) to a string.
使用UTCString()来将今天的日期转换成字符串(依据UTC)

getDay()
Use getDay() and an array to write a weekday, and not just a number.
使用getDay()和一数组来书写星期几,并不是简单的数字

Display a clock
How to display a clock on your web page.
怎样在你的页上显示一个时钟

Tip: You will find a lot more examples in the Date object reference - look at the bottom of this page!
提示:你将在Date object reference找到更多的例子(页面底部有连接)


Defining Dates
定义日期

The Date object is used to work with dates and times.
时间对象用来操作日期跟时间

We define a Date object with the new keyword. The following code line defines a Date object called myDate:
我们通过一个新的关键字来定义一个时间对象。下面的代码行就定义了一个时间对象(称作myDate):

var myDate=new Date()

Note: The Date object will automatically hold the current date and time as its initial value!
注意:时间对象将自动把当前的日期和时间作为初始值!


Manipulate Dates
操作(设置)时间

We can easily manipulate the date by using the methods available for the Date object.
我们可以轻易地用有效的时间对象来操作(设置)日期或时间

In the example below we set a Date object to a specific date (14th January 2010):
在下面的例子中我们设置一个时间对象来指定日期(2010年一月14号):

var myDate=new Date()
myDate.setFullYear(2010,0,14)

And in the following example we set a Date object to be 5 days into the future:
下面的例子我们将一时间对象设置为未来的5天:

var myDate=new Date()
myDate.setDate(myDate.getDate()+5)

Note: If adding five days to a date shifts the month or year, the changes are handled automatically by the Date object itself!
注意:如果给一日期增加天改为给月或年增加的话,一些变化会由时间对象自动处理!


Comparing Dates
比较时间(日期)

The Date object is also used to compare two dates.
时间对象同样使用在比较两个日期(时间)

The following example compares today's date with the 14th January 2010:
下面的例子将今天的日期和2010年1月14号作比较:

var myDate=new Date()
myDate.setFullYear(2010,0,14)
var today = new Date()
if (myDate>today)
alert("Today is before 14th January 2010")
else
alert("Today is after 14th January 2010")

 


Complete Date Object Reference
完整的时间对象参考

For a complete reference of all the properties and methods that can be used with the Date object, go to our complete Date object reference.
Date[日期]对象中的所有属性和方法参数,我们将在 完整Date对象参数 中罗列说明。

The reference contains a brief description and examples of use for each property and method!
我们将列举简要说明和典型案例来讲解每个参数的属性和方法的用法。

评论 (0) All

登陆 | 还没注册?