专注于技术经验交流

水至清则无鱼、宁静而致远!

技术、经验、学习共同打造网络新生活!
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

JavaScript学习与实践(2)

Posted on 2007-02-01 17:01  小鱼儿  阅读(160)  评论(0编辑  收藏  举报
js中 Date对象

     这个对象主要是用于操作日期和时间的

例子:用Date()得到当前的日期

  

<html>
<body>
<script type="text/javascript">
document.write(Date())
</script>
</body>
</html>
下面一个getTime()
用他来计算从1970年一共过了多少年
 
<html>
<body>
<script type="text/javascript">
var minutes = 1000*60
var hours = minutes*60
var days = hours*24
var years = days*365
var d = new Date()
var t = d.getTime()
var y = t/years
document.write("It's been: " + y + " years since 1970/01/01!")
</script>
</body>
</html>
下面一个是setFullYear(),用他来设置一个特殊的日期
<html>
<body>
<script type="text/javascript">
var d = new Date()
d.setFullYear(1992,10,3)
document.write(d)
</script>
</body>
</html>
下面是一个toUTCString(),他用于把日期转换成UTC格式
<html>
<body>
<script type="text/javascript">
var d = new Date()
document.write (d.toUTCString())
</script>
</body>
</html>
下面是一个getDay()用这个和一个数组来返回一个星期,不仅仅是一个数字哦,
<html>
<body>
<script type="text/javascript">
var d=new Date()
var weekday=new Array(7)
weekday[0]="Sunday"
weekday[1]="Monday"
weekday[2]="Tuesday"
weekday[3]="Wednesday"
weekday[4]="Thursday"
weekday[5]="Friday"
weekday[6]="Saturday"
document.write("Today it is " + weekday[d.getDay()])
</script>
</body>
</html>
怎么在一个页面上展现一个时钟呢,下面的代码可以帮助我们
<html>
<head>
<script type="text/javascript">
function startTime()
{
var today=new Date()
var h=today.getHours()
var m=today.getMinutes()
var s=today.getSeconds()
// add a zero in front of numbers<10
m=checkTime(m)
s=checkTime(s)
document.getElementById('txt').innerHTML=h+":"+m+":"+s
t=setTimeout('startTime()',500)
}
function checkTime(i)
{
if (i<10)
{i="0" + i}
return i
}
</script>
</head>
 

Date Object Methods

FF: Firefox, N: Netscape, IE: Internet Explorer

MethodDescriptionFFNIE
Date() Returns today's date and time 1 2 3
getDate() Returns the day of the month from a Date object (from 1-31) 1 2 3
getDay() Returns the day of the week from a Date object (from 0-6) 1 2 3
getMonth() Returns the month from a Date object (from 0-11) 1 2 3
getFullYear() Returns the year, as a four-digit number, from a Date object 1 4 4
getYear() Returns the year, as a two-digit or a four-digit number, from a Date object. Use getFullYear() instead !! 1 2 3
getHours() Returns the hour of a Date object (from 0-23) 1 2 3
getMinutes() Returns the minutes of a Date object (from 0-59) 1 2 3
getSeconds() Returns the seconds of a Date object (from 0-59) 1 2 3
getMilliseconds() Returns the milliseconds of a Date object (from 0-999) 1 4 4
getTime() Returns the number of milliseconds since midnight Jan 1, 1970 1 2 3
getTimezoneOffset() Returns the difference in minutes between local time and Greenwich Mean Time (GMT) 1 2 3
getUTCDate() Returns the day of the month from a Date object according to universal time (from 1-31) 1 4 4
getUTCDay() Returns the day of the week from a Date object according to universal time (from 0-6) 1 4 4
getUTCMonth() Returns the month from a Date object according to universal time (from 0-11) 1 4 4
getUTCFullYear() Returns the four-digit year from a Date object according to universal time 1 4 4
getUTCHours() Returns the hour of a Date object according to universal time (from 0-23) 1 4 4
getUTCMinutes() Returns the minutes of a Date object according to universal time (from 0-59) 1 4 4
getUTCSeconds() Returns the seconds of a Date object according to universal time (from 0-59) 1 4 4
getUTCMilliseconds() Returns the milliseconds of a Date object according to universal time (from 0-999) 1 4 4
parse() Takes a date string and returns the number of milliseconds since midnight of January 1, 1970 1 2 3
setDate() Sets the day of the month in a Date object (from 1-31) 1 2 3
setMonth() Sets the month in a Date object (from 0-11) 1 2 3
setFullYear() Sets the year in a Date object (four digits) 1 4 4
setYear() Sets the year in the Date object (two or four digits). Use setFullYear() instead !! 1 2 3
setHours() Sets the hour in a Date object (from 0-23) 1 2 3
setMinutes() Set the minutes in a Date object (from 0-59) 1 2 3
setSeconds() Sets the seconds in a Date object (from 0-59) 1 2 3
setMilliseconds() Sets the milliseconds in a Date object (from 0-999) 1 4 4
setTime() Calculates a date and time by adding or subtracting a specified number of milliseconds to/from midnight January 1, 1970 1 2 3
setUTCDate() Sets the day of the month in a Date object according to universal time (from 1-31) 1 4 4
setUTCMonth() Sets the month in a Date object according to universal time (from 0-11) 1 4 4
setUTCFullYear() Sets the year in a Date object according to universal time (four digits) 1 4 4
setUTCHours() Sets the hour in a Date object according to universal time (from 0-23) 1 4 4
setUTCMinutes() Set the minutes in a Date object according to universal time (from 0-59) 1 4 4
setUTCSeconds() Set the seconds in a Date object according to universal time (from 0-59) 1 4 4
setUTCMilliseconds() Sets the milliseconds in a Date object according to universal time (from 0-999) 1 4 4
toSource() Represents the source code of an object 1 4 -
toString() Converts a Date object to a string 1 2 4
toGMTString() Converts a Date object, according to Greenwich time, to a string. Use toUTCString() instead !! 1 2 3
toUTCString() Converts a Date object, according to universal time, to a string 1 4 4
toLocaleString() Converts a Date object, according to local time, to a string 1 2 3
UTC() Takes a date and returns the number of milliseconds since midnight of January 1, 1970 according to universal time 1 2 3
valueOf() Returns the primitive value of a Date object 1 2 4


Date Object Properties

PropertyDescriptionFFNIE 
constructor A reference to the function that created the object 1 4 4
prototype Allows you to add properties and methods to the object 1 3 4
New Document