自己封装的data日期工具类

HTML页面的调用

<!DOCTYPE html>
<html>

    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <script src="js/DateUntils.js" type="text/javascript" charset="utf-8"></script>

    <body>
        <div id="strTime">

        </div>
    </body>
    <script type="text/javascript">
        // 获取当前时间的日(年-月-日 时:分:秒)
        console.log(dateUntils.datetime());
        // 获取当前时间的(年-月-日)
        console.log(dateUntils.date());
        // 获取当前时间的( 时:分:秒)
        console.log(dateUntils.time());
        console.log("---------")
        // 获取指定时间的详细信息
        dateUntils.name = "2015-05-11";
//        dateUntils.name = new Date();        
        console.log(dateUntils.datetime());
        console.log(dateUntils.date());
        console.log(dateUntils.time());
        console.log("---------")
        /*
         * 当前时间
         * num 默认为0;获取的是今天日期
         */
        console.log(dateUntils.appointedDay());
        // 获取五天前的日期
        dateUntils.num = "-5";
        console.log(dateUntils.appointedDay());
        //获取五天后的日期
        dateUntils.num = "5";
        console.log(dateUntils.appointedDay());
        console.log("-------------");
        /**
         * 活动日期
         */
        // 设置开始时间
        dateUntils.startDate = "2018-02-15";
        // 设置结束时间
        dateUntils.endDate = "2018-05-11";
        // 开始活动时间事件 默认为关闭
        dateUntils.bool = true;
        // 设置 活动返回值接收的id
        dateUntils.id = "strTime";
        // 调用事件。如果bool为false,那么这个方法不会执行
        dateUntils.startAndEnd();
    </script>

</html>

DateUntils js工具类

/**
 * 获取当前日期的时间格式
 */
var dateUntils = {
    name: "", // 指定时间
    num: 0, // 多少天前后
    startDate: "", // 活动开始时间
    endDate: "", // 活动结束时间
    id : "",
    bool: false,
    startAndEnd: function(){
        if (this.bool && this.id != "" || this.id != null) {
            start(this.id);
        } else {
            console.log("id没有设置,或者bool值没有设置为true");
        }
    }, // 是否进行活动
    type: function() {
        return this.name == "" ? fDate(new Date()) : fDate(this.name);
    },
    datetime: function() {
        return this.type().year + "-" + this.type().month + "-" + this.type().day + " " +
            this.type().hour + ":" + this.type().minute + ":" + this.type().second;
    },
    date: function() {
        return this.type().year + "-" + this.type().month + "-" + this.type().day;
    },
    time: function() {
        return this.type().hour + ":" + this.type().minute + ":" + this.type().second;
    },
    appointedDay: function() {
        return appointedDate(this.num);
    }
}
/**
 * 根据num值进行获取指定几天前或几天后的日期
 * @param {Object} 必须为number类型 可以为负数和整数
 */
function appointedDate(num) {
    var d = new Date();
    var newDate = d.getTime() + 1000 * 60 * 60 * 24 * num;
    var mydate = new Date(newDate);
    var str = mydate.getFullYear() + "-";
    str += toTwo(mydate.getMonth() + 1) + "-";
    str += toTwo(mydate.getDate());
    return str;
}
/**
 * 根据输入的时间进行date日期转换
 * @param {Object} 手动输入的时间
 */

function fDate(d) {
    // 创建当前时间
    var date = new Date(d);
    // 获取当前时间的年份
    var year = date.getFullYear();
    //  获取当前时间的月份(0-11),当前月份加一为实际月份
    var month = date.getMonth() + 1;
    //  获取当前时间的日期(天数
    var day = date.getDate();
    //  获取当前时间的小时数
    var hour = date.getHours();
    //      获取当前时间的分钟数
    var minute = date.getMinutes();
    //     获取当前时间的秒数
    var second = date.getSeconds();
    return {
        year: year,
        month: toTwo(month),
        hour: toTwo(hour),
        day: toTwo(day),
        hour: toTwo(hour),
        minute: toTwo(minute),
        second: toTwo(second)
    }
}

/**
 * 判断当前数字是否小于10,如果小于10,则在数字前面加0
 */
function toTwo(t) {
    return t >= 10 ? t : "0" + t;
}
/**
 * 获取还剩下多少时间开始或结束
 * @param {Object} 秒数
 */
function timeCountDown(time) {
    var day = Math.floor(time / (60 * 60 * 24));
    var hour = Math.floor((time - day * 24 * 60 * 60) / 3600);
    var minute = Math.floor((time - day * 24 * 60 * 60 - hour * 3600) / 60);
    var second = Math.floor(time - day * 24 * 60 * 60 - hour * 3600 - minute * 60);
    return toTwo(day) + "天" + toTwo(hour) + "小时" + toTwo(minute) + "分" + toTwo(second) + "秒";
}
/**
 * 页面显示的地方
 * @param {Object}  页面接收的id值
 */
function ShowCountDown(divname) {
    var now = new Date();
    var start = new Date(dateUntils.startDate);
    var end = new Date(dateUntils.endDate);
    var startTime = (start.getTime() - now.getTime()) / 1000;
    var endtTime = (end.getTime() - now.getTime()) / 1000;
    var str;
    if(startTime >= 0) {
        str = "活动尚未开始,还有:" + timeCountDown(startTime) + "开始 ";
    } else if(endtTime > 0) {
        str = "活动正在进行中,还有:" + timeCountDown(endtTime) + "结束 ";
    } else {
        str = "活动已结束";
        window.clearInterval(activityTime);
    }
    var cc = document.getElementById(divname);
    cc.innerHTML = str;
}
function start(id){
    ShowCountDown(id);
    var activityTime = window.setInterval(function() {
        ShowCountDown(id);
    }, 1000);
}

若在使用中出现问题,请联系我

posted @ 2018-04-28 11:56  幺丸  阅读(494)  评论(0编辑  收藏  举报