js 基础之Date对象

在js中是通过 Date对象来获取当前时间的 例如: var nowTime = new Date();

  具体的一些方法是:

        

                                                                    所有的set对应都有get系列 具体在http://www.w3school.com.cn/jsref/jsref_obj_date.asp

  具体用法如下:

var date = new Date(),
    nowYear = date.getFullYear(),
    nowMonth = date.getMonth() + 1,  //注意getMonth从0开始,getDay()也是(此时0代表星期日)
    nowDay = date.getDate(),
    nowHour = date.getHours(),
    nowMinute = date.getMinutes(),
    nowSecond = date.getSeconds(),
    weekday = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"],
    nowWeek = weekday[date.getDay()];
console.log(nowYear + '年' + nowMonth + '月' + nowDay + '日' + nowHour + '时' + nowMinute + '分' + nowSecond + '秒' + nowWeek);

利用内置的 setInterval 或者 setTimeout 回调自身的方法,可以很容易的做时钟效果 具体如下:

      //当分钟和秒数是一位的时候 补齐成两位      
      function
checkTime(i){ if(i<10){ i = "0"+i; } return i; } function nowTime(){ var text = document.getElementById("sj"); var mytime = new Date(); var h = mytime.getHours(); var m = mytime.getMinutes(); var s = mytime.getSeconds(); m = checkTime(m); s = checkTime(s); text.innerHTML = "现在是:"+h+":"+m+":"+s; } setInterval('nowTime()',1000);

 

posted @ 2016-03-24 15:01  AxinBYZ  阅读(143)  评论(0)    收藏  举报