Fork me on GitHub

常用JS - JQuery版

秒转为时分秒格式

function formatSeconds(value) {
            if(value == undefined)
            {
                value = 0;
            }    
            var second = parseInt(value);// 秒
            var min = 0;// 分
            var hour = 0;// 小时
            if(second > 60) {
                min = parseInt(second/60);
                second = parseInt(second%60);
                if(min > 60) {
                    hour = parseInt(min/60);
                    min = parseInt(min%60);
                    }
                }
            var result = ""+parseInt(second)+"秒";
            if(min > 0) {
                result = ""+parseInt(min)+"分"+result;
            }
            if(hour > 0) {
                result = ""+parseInt(hour)+"小时"+result;
            }
            return result;
        } 

根据起止日期计算日期数组

    //日期类型转换为年月日字符串
    function formatDate(date) {
        var year = date.getFullYear(); // 获取年份
        var month = date.getMonth() + 1; // 获取月份,加1使之从1开始而不是0
        var day = date.getDate(); // 获取日期

        // 如果月份小于10,则在前面添加0
        month = month < 10 ? '0' + month : month;

        // 如果日期小于10,则在前面添加0
        day = day < 10 ? '0' + day : day;

        // 拼接成yyyy-mm-dd格式的字符串
        return year + '-' + month + '-' + day;
    }

    // 根据起止时间,返回日期数组
    function getDatesBetween(startDate, endDate) {
        // 创建一个空数组来存储日期
        var dates = [];

        // 将开始日期转换为日期对象
        var currentDate = new Date(startDate);

        // 将结束日期转换为日期对象
        endDate = new Date(endDate);

        // 循环遍历日期范围
        while (currentDate <= endDate) {
            // 将当前日期添加到数组中
            dates.push(formatDate(new Date(currentDate)));
            // currentDate增加一天
            currentDate.setDate(currentDate.getDate() + 1);
        }

        // 返回日期数组
        return dates;
    }

按钮选中效果JS

$(document).ready(function() {
        // 监听 .container_main 下的 div 的点击事件
        $('.container_main>.activeble_btn').on('click', function() {
            // 移除所有的 .button_active 类
            $('.activeble_btn').removeClass('button_active').addClass('button_noactive');

            // 为被点击的 div 的子元素添加 button_active 类
            $(this).addClass('button_active').removeClass('button_noactive');
        });
    });
posted @ 2018-06-15 15:17  秋夜雨巷  阅读(1960)  评论(0编辑  收藏  举报