前端项目日常开发之业务逻辑集成

作为一名一线开发人员,经历过几个项目之后,即可发现其实每个项目都会有相同的业务处理。如,在与后台进行数据交互中,将时间转成时间戳传给后台;前端界面优化中,数据加载时要有遮罩提示用户当前数据正在加载中,当提交数据提示用户提交成功或者失败等提示模态框。


时间方面:


var dateTime = {
     //获取当天日期
    onGetCurrentDate: function () {
        var date = new Date(),
            year = date.getFullYear(),
            month = date.getMonth() + 1,
            day = date.getDate();
        month = month < 10 ? ('0' + month) : month;
        day = day < 10 ? ('0' + day) : day;
        return year + '-' + month + '-' + day;
    },
    //时间戳转日期
    getFormatTimesTamp: function (val) {

        if (val > 2554431132000 || val == 0 || val == undefined) return '-';

        var date = new Date(val);
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }

        var hours = date.getHours();
        if (hours >= 0 && hours <= 9) {
            hours = "0" + hours;
        }

        var min = date.getMinutes();
        if (min >= 0 && min <= 9) {
            min = "0" + min;
        }

        var seconds = date.getSeconds();
        if (seconds >= 0 && seconds <= 9) {
            seconds = "0" + seconds;
        }
        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
            " " + hours + seperator2 + min + seperator2 + seconds;
        return currentdate;
    },
    //时间戳转日期 不需要时间字段
    getFormatDateTamp: function (val, seperator) {
        if (val > 2554431132000 || val == 0 || val == null) return '-';

        var date = new Date(val);
        seperator = seperator || "-";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }

        var currentdate = date.getFullYear() + seperator + month + seperator + strDate

        return currentdate;
    },
    //时间戳转为日期 不需要年月日字段
    getFormatTamp: function (val) {
        if (val > 2554431132000 || val == 0) return '-';

        var date = new Date(val);

        var seperator2 = ":";

        var hours = date.getHours();

        if (hours >= 0 && hours <= 9) {
            hours = "0" + hours;
        }

        var min = date.getMinutes();
        if (min >= 0 && min <= 9) {
            min = "0" + min;
        }

        var currentdate = hours + seperator2 + min;
        return currentdate;
    },
    //时间戳转日期,不要秒字段
    getFormatTimesNotSecond: function (val) {

        if (val > 2554431132000 || val == 0 || val == undefined) return '-';

        var date = new Date(val);
        var seperator1 = "-";
        var seperator2 = ":";
        var month = date.getMonth() + 1;
        var strDate = date.getDate();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }

        var hours = date.getHours();
        if (hours >= 0 && hours <= 9) {
            hours = "0" + hours;
        }

        var min = date.getMinutes();
        if (min >= 0 && min <= 9) {
            min = "0" + min;
        }

        var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate +
            " " + hours + seperator2 + min;
        return currentdate;
    },
    //日期转为时间戳
    getDateTimesTamp: function (date) {
        if (!date) return 0;
        date = date.substring(0, 19);
        date = date.replace(/-/g, '/');
        return new Date(date).getTime();
    },
   
};

你可能感兴趣的:(日常开发)