Date对象

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

一般倒计时提示会用到该对象的一些方法。

获取当前的年、月、日、星期、小时、分钟、秒、毫秒

var getToday = new Date(),
            getyear = getToday.getFullYear(),//获取年份完整格式 如2015
            getmonth = getToday.getMonth() + 1,//返回月份 从0开始-11
            getday = getToday.getDate(),//返回日期1-31
            getweek = getToday.getDay(),//返回星期0-6
            gethour = getToday.getHours(),//返回小时数
            getminutes = getToday.getMinutes(),//返回分钟数
            getseconds = getToday.getSeconds(),//返回秒数
            gettime = getToday.getTime();//返回毫秒数
        var weekArr = new Array(7);
            weekArr[0] = "星期日";
            weekArr[1] = "星期一";
            weekArr[2] = "星期二";
            weekArr[3] = "星期三";
            weekArr[4] = "星期四";
            weekArr[5] = "星期五";
            weekArr[6] = "星期六";
        console.log(getyear + "年 " + getmonth + "月 " + getday + "日 " + weekArr[getweek] + " " + gethour + "时 " + getminutes + "分 " + getseconds + "秒 " + gettime + "毫秒 ");

 

倒计时抢购带倒计时秒钟即时同步改变。

测试用的jquery外层有$(function() {}), javascript需要window.onload = funtion() { showSeconds();}

 

function showSeconds() {
            var endTime = new Date("2015/2/28,12:00:00");
            var getToday = new Date();
            var out = document.getElementById("out");
            var leftTime = (endTime.getTime() - getToday.getTime())/1000,
                leftDay = parseInt(leftTime/(24*60*60)),
                leftHour = parseInt(leftTime/(60*60)%24),
                leftMinutes = parseInt(leftTime/60%60),
                leftSeconds = parseInt(leftTime%60);
            out.innerHTML = "距离2015/2/28,12:00:00还剩 " + leftDay + "天" + leftHour + "时" + leftMinutes + "分" + leftSeconds + "秒";
            setTimeout(showSeconds, 1000);
        }
        showSeconds();

 

posted on 2015-02-21 12:56  比比已存在  阅读(204)  评论(0编辑  收藏  举报