js获取两个时间差

来源:互联网 发布:如何筛选excel重复数据 编辑:程序博客网 时间:2024/06/05 00:24
//获取 n 天前的日期
        function getDate(val) {
            var dateGrade = new Date().getTime() - val * 24 * 60 * 60 * 1000; //计算时间差
            var odate = new Date(dateGrade); //时间差转换为标准时间
            var Y = odate.getFullYear() < 10 ? "0" + odate.getFullYear() : odate.getFullYear() + ""; //获取年份
            var M = (odate.getMonth() + 1) < 10 ? "0" + (odate.getMonth() + 1) : (odate.getMonth() + 1) + ""; //获取月份
            var D = odate.getDate() < 10 ? "0" + odate.getDate() : odate.getDate() + ""; //获取日期
            var ndate = Y + M + D;
            return ndate;
        }


        console.log(getDate(7) + "----" + getDate(0));
原创粉丝点击