Date 对象的总结与案例

Original 2018-11-05 10:31:21 283
abstract:Date 对象是用于处理日期与时间。创建Date对象的方法:var myday= new Date();Date对象方法有getFullYear():从 Date 对象以四位数字返回年份getMonth():从 Date 对象返回月份 (0 ~ 11),0代表的是1月 11代表的是12月。getDay():从 Date 对象返回一周中的某一天 (0 ~ 6),0代表的

Date 对象是用于处理日期与时间。

创建Date对象的方法:

var myday= new Date();

Date对象方法有


getFullYear():从 Date 对象以四位数字返回年份

getMonth():从 Date 对象返回月份 (0 ~ 11),0代表的是1月 11代表的是12月。

getDay():从 Date 对象返回一周中的某一天 (0 ~ 6),0代表的是周一,6代表的是周日。

getDate():从 Date 对象返回一个月中的某一天 (1 ~ 31)。

getHours():返回 Date 对象的小时 (0 ~ 23)。

getMinutes():返回 Date 对象的分钟 (0 ~ 59)。

getSeconds():返回 Date 对象的秒数 (0 ~ 59)。

案例:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DATE</title>
</head>
<body>
<script type="text/javascript">
var myday= new Date();
document.write(myday+'<br>')
//获取年份 getFullYear() 只有获取当前日期的时候才能获取到年份
document.write(myday.getFullYear()+"年"+'<br>')
//获取月份 getMonth() 0代表的是1月 11代表的是12月
document.write(myday.getMonth()+"月"+'<br>')
var month= new Array(12)
month[0]="一月"
month[1]="二月"
month[2]="三月"
month[3]="四月"
month[4]="五月"
month[5]="六月"
month[6]="七月"
month[7]="八月"
month[8]="九月"
month[9]="十月"
month[10]="十一月"
month[11]="十二月"
document.write("当前月是"+month[myday.getMonth()]+'<br>')
//获取星期 0是周日 6是周六
document.write("星期"+myday.getDay()+'<br>')
//获取日期 返回的是0-31的整数
document.write("今天是这个月的第"+myday.getDate()+"天")
//获取小时 getHours() 返回的是0-23的整数
document.write("现在是"+myday.getHours()+"时"+'<br>')
//获取分钟 getMinutes() 返回的是0-59的整数
document.write("现在是"+myday.getMinutes()+"分"+'<br>')
// 获取秒钟 getSeconds() 返回的是0-59的整数
document.write("现在是"+myday.getSeconds()+"秒"+'<br>')
</script>
</body>
</html>


  


Correcting teacher:灭绝师太Correction time:2018-11-05 11:11:27
Teacher's summary:知识面到现在的话,自己学习应该没问题了,除了上课的知识点,有空余时间可以自己扩展点知识,有问题及时反馈

Release Notes

Popular Entries