日期操作 & 数学函数

课程目标

  • 掌握日期函数的使用
  • 掌握数学函数的使用

课程内容

日期的使用

//创建日期对象,当前日期
var d = new Date();   
console.log(d);
//设置日期,创建日期对象
var d1 = new Date('5/12/2014');
//设置日期,时分秒
var d2 = new Date(2014, 5, 18, 22, 23, 40);
var date = d1.getDate(), //从日期对象中获取一月中得天数, 1~31, 
    month = d1.getMonth(), //获取月份,注意从0开始,一月份的值是0, 0~11,
    year = d1.getFullYear(), //获取年份,2014,
    hour = d1.getHours(), //获取小时, 0~23,
    min = d1.getMinutes(), //获取分,0~59,
    sec = d1.getSeconds(), //获取秒,0~59,
    milSec = d1.getMillseconds(), //获取毫秒,0~999
    day = d1.getDay(); //获取星期几,周日是0,周一是1, 0~6


//单独设置年份
var d3 = new Date();
    d3.setFullYear(2014);


//1970.1.1 到当前日期的毫秒数
var oldDate = new Date(2014, 11, 21),
    time = oldDate.getTime(); 
var time2 = new Date().getTime();
var during = time2 - time;
console.log(during);

var start = Date.now();  // ie9+ 
for(var i = 0; i< 10000; i++){
  console.log(i);
}
var end = Date.now();
alert(end - start);

var start2 = +new Date();
var end2 = +new Date();
console.log(end2 - start2);

练习1

写一个函数,把数字日期如2014-06-21转换为 二零一四年六月二十一日星期三

function convert( dateStr ){
    var dict = {
        "0": "零",
        "1": "一",
        "2": "二",
        "3": "三",
        "4": "四",
        "5": "五",
        "6": "六",
        "7": "七",
        "8": "八",
        "9": "九",
        "10": "十",
        ...
    };
    var date = dateStr.split('-'),
        yy = date[0],
        mm = date[1],
        dd = date[2];

    var yearStr = dict[yy[0]] + dict[yy[1]] + dict[yy[2]] + dict[yy[3]] + '年',
        monthStr = dict[''+Number(mm)] + '月',
        dayStr = dict[dd];
}

练习2

写一个函数,返还n天前的日期,如今天是2014-11-22,7天前的日期是2014-11-15

function getDaysBefore(n){
    var nowDate = new Date(),
        nowTime = nowDate.getTime();
    var nDayTime = n * 1000 * 60 * 60 * 24,
        nDaysBeforeTime = nowTime - nDayTime,
        d = new Date(nDaysBeforeTime);
    return d.getFullYear() + '-' + (d.getMonth() + 1) + '-' + d.getDate();
} 

数学函数

数学常用函数的使用

var n = Math.random();
Math.ceil(2.34);
Math.floor(3.33);
Math.max(100, 90);
Math.min(100, 90);
Math.round(2.35)//

练习 写一个函数,输入为两个整数,返回两个整数之间的随机数:如1,100 返回1到99的随机数

function random( num1, num2){
    return num1 + (Math.floor(Math.random() * (num2 - num1)));
}

练习,写一个倒计时

//倒计时
//
function countDown (dateStr) {
    var end = +new Date(dateStr),
        start = +new Date(),
        during = Math.floor((end - start)/1000);
    /**
        1天10小时20分15秒 
        day*24*60*60 + hour*60*60 + min*60 + sec

        day = Math.floor( during/(24*60*60) );
        var t1 = during - (day*24*60*60);
        hour = Math.floor( t1/(60*60) );
        var t2 = t1 - hour*60*60;
        min = Math.floor( t2/60 );
        sec = t2%60;

     */
    var day = Math.floor( during/(24*60*60) ),
        t1 = during - (day*24*60*60),
        hour = Math.floor( t1/(60*60) ),
        t2 = t1 - hour*60*60,
        min = Math.floor( t2/60 ),
        sec = t2%60;

    return day + '天' + hour + '小时' + min + '分' + sec + '秒';
}

setInterval(function(){
    var str = countDown('2014-11-23');
    document.write(str);
}, 1000);

布尔变量

以下布尔判断均为false

0
-0
null
""
false
undefined
NaN