js获取系统当前小时展示不同的时间状态

JavaScript020

js获取系统当前小时展示不同的时间状态,第1张

获取系统当前小时展示不同的时间状态

data(){

return{

sayHello:undefined

}}

constnow=newDate()

consthour=now.getHours()

if(hour<6) {

this.sayHello='凌晨好'

}elseif(hour<9) {

this.sayHello='早上好'

}elseif(hour<12) {

this.sayHello='上午好'

}elseif(hour<14) {

this.sayHello='中午好'

}elseif(hour<17) {

this.sayHello='下午好'

}elseif(hour<19) {

this.sayHello='傍晚好'

}elseif(hour<22) {

this.sayHello='晚上好'

}else{

this.sayHello='夜里好'

}

},

需要准备的材料分别有:电脑、html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html。

2、在index.html中的<script>标签,输入js代码:

var a = new Date()document.body.innerHTML

= '时:' + a.getHours() + '<br/>分:' + a.getMinutes() + '<br/>秒:' + a.getSeconds()

3、浏览器运行index.html页面,此时当前时间的时分秒都被js获取并打印了出来。