日期函数
getMonth Returns local month (0––11)
getSeconds
Returns local second (0––59)
getTime
Returns number of seconds since January 1, 1970 00:00:00 UTC
getTimezoneOffset
Returns time zone from UTC
getUTCDate
Returns day of month in UTC time (0––31) method (Date)
getUTCDay
Returns day of the week in UTC time (0––6)
getUTCFullYear
Returns 4-digit UTC year
getUTCHours
Returns UTC hours (0––23)
getUTCMilliseconds
Returns UTC milliseconds (0––999)
getUTCMinutes
Returns UTC minutes (0––59)
getUTCMonth
Returns UTC month (0––11)
getUTCSeconds
Returns UTC seconds (0––59)
setDate
Sets the day of month (1––31)
setFullYear
Sets 4-digit full year
setHours
Sets the hour (0––23)
setMilliseconds
Sets the date’’s milliseconds (0––999)
setMinutes
Sets the date’’s minutes (0––59)
setMonth
Sets the month (0––11)
setSeconds
Sets the seconds (0––59)
setTime
Sets the date’’s time as milliseconds since January 1, 1970 00:00:00 UTC
setUTCDate
Sets the date’’s day of month in UTC
setUTCFullYear
Sets the full year in UTC
setUTCHours
Sets the date’’s hours in UTC
setUTCMilliseconds
Sets the date’’s milliseconds in UTC
setUTCMinutes
Sets the date’’s minutes in UTC
setUTCMonth
Sets the month in UTC
setUTCSeconds
Sets the seconds in UTC
格式化输出
<script>
window. {
var dt = new Date();
// get month and increment
var mnth = dt.getUTCMonth();
mnth++;
var day = dt.getUTCDate();
if (day < 10) day="0" + day;
var yr = dt.getUTCFullYear();
var hrs = dt.getUTCHours();
if (hrs < 10) hrs = "0" + hrs;
var min = dt.getUTCMinutes();
if (min < 10) min = "0" + min;
var secs = dt.getUTCSeconds();
if (secs < 10) secs = "0" + secs;
var newdate = yr + "-" + mnth + "-" + day + " " + hrs + ":" + min + ":" + secs + "";
alert(newdate);
}
</script>
创建指定日期
<script>
window. {
var month = 10; // Month 11, in zero based system, is November
var day = 18;
var year = 1954;
var dt = new Date(year,month,day); // time is set to zero by default
alert(dt);
}
</script>
日期运算
两个日期可以直接相减,返回相差的毫秒值。其他操作,如加,乘,除都没有意义。
<script>
var firstDate;
window.onload=startTimer;
function startTimer(){
firstDate = new Date();
document.getElementById("date").onclick=doEvent;
}
function doEvent() {
var secondDate = new Date();
alert((secondDate - firstDate) / 1000);
}
</script>
Timeout 函数
<script>
window. {
setTimeout("alert('timeout!')",3000);
}
</script>
定时执行
#redbox
{
position: absolute;
left: 100px;
top: 100px;
width: 200px; height: 200px;
background-color: red;
}
</style>
<script>
var intervalId=null;
window. {
document.getElementById("redbox").onclick=stopStartElement;
}
function stopStartElement() {
if (intervalId == null) {
var x = 100;
intervalId=setInterval(function() {
x+=5;
var left = x + "px";
document.getElementById("redbox").style.left=left;}, 100);
} else {
clearInterval(intervalId);
intervalId=null;
}
}
</script>