JavaScript-常用的时间、日期操作

229 阅读2分钟
获取当前日期时间戳

//方式一(毫秒)
const todayStamp = new Date().getTime();

//方式二(毫秒)
const todayStamp = Date.now();

//方式三(毫秒)
const todayStamp = (new Date()).valueOf();

//方式四(毫秒)
const todayStamp = +new Date();

//方式五(秒)
const todayStamp = Date.parse(new  Date());
指定日期转换为时间戳

const timeDate = "2020/01/01 12:00:00",
timeStamp = new Date(timeDate).getTime();
获取当前时间N天后的时间戳

function toNextTimeStamp(n) {    
    let timestamp = Date.now() + n * 86400000;    
    return timestamp;
}

获取当前时间 / 将时间戳转换为日期      返回格式:2020-01-01 08:00:00

function timeStampConvert(timeStamp = new Date(), sep = '-') {    
    let year = timeStamp.getFullYear(),        //获取年份        
        month = timeStamp.getMonth() + 1,      //获取月份(0-11,0代表1月)        
        date = timeStamp.getDate(),            //获取日期        
        hour = timeStamp.getHours(),           //获取小时(0-23)        
        minutes = timeStamp.getMinutes(),      //获取分钟数(0-59)        
        seconds = timeStamp.getSeconds();      //获取秒数(0-59)    
    month = month < 10 ? '0' + month : month;    
    date = date < 10 ? '0' + date : date;    
    hour = hour < 10 ? '0' + hour : hour;    
    minutes = minutes < 10 ? '0' + minutes : minutes;    
    seconds = seconds < 10 ? '0' + seconds : seconds;    
    return year + sep + month + sep + date + ' ' + hour + ':' + minutes + ':' + seconds;
}

//用法示例一:获取当前时间
timeStampConvert()        //返回当前日期 如:2020-01-01 17:00:24

//用法示例二:时间戳转换为日期
timeStampConvert(new Date(timeStamp))
获取本周第一天

function showWeekFirstDay() {    
    let Nowdate = new Date();    
    let WeekFirstDay = new Date(Nowdate - (Nowdate.getDay() - 1) * 86400000);    
    let month = WeekFirstDay.getMonth() + 1,        
        date = WeekFirstDay.getDate();    
    month = month < 10 ? '0' + month : month;    
    date = date < 10 ? '0' + date : date;    
    return WeekFirstDay.getFullYear() + '-' + month + '-' + date;
}

获取本周最后一天

function showWeekLastDay() {    
    let Nowdate = new Date();    
    let WeekFirstDay = new Date(Nowdate - (Nowdate.getDay() - 1) * 86400000);    
    let WeekLastDay = new Date((WeekFirstDay / 1000 + 6 * 86400) * 1000);    
    let month = WeekLastDay.getMonth() + 1,        
        date = WeekLastDay.getDate();    
    month = month < 10 ? '0' + month : month;    
    date = date < 10 ? '0' + date : date;    
    return WeekLastDay.getFullYear() + '-' + month + '-' + date;
}

获取与当前时间的间隔

function getTimeInterval(time) {    //注意:进行时间差运算的时间 年月日之间需要用/隔开  比如:2020/01/20 13:00:21
    let time1 = new Date(timeStampConvert().replace(/-/g, "/")),        
        time2 = new Date(time.replace(/-/g, "/")),        
        time3 = time1.getTime() - time2.getTime();  //时间差的毫秒数    
    //计算出相差天数    
    let days = Math.floor(time3 / (24 * 3600 * 1000));    
    //计算天数后剩余的毫秒数    
    let leave1 = time3 % (24 * 3600 * 1000);
     //计算出小时数    
    let hours = Math.floor(leave1 / (3600 * 1000));    
    //计算小时数后剩余的毫秒数    
    let leave2 = leave1 % (3600 * 1000);        
    //计算相差分钟数   
    let minutes = Math.floor(leave2 / (60 * 1000));    
    //计算分钟数后剩余的毫秒数    
    let leave3 = leave2 % (60 * 1000);      
    //计算相差秒数  
    let seconds = Math.round(leave3 / 1000);    
    let returnValue = time;    
    if(days > 0){        
        returnValue = days + '天前';    
    }else if (hours > 0){        
        returnValue = hours + '小时前';    
    }else if (minutes > 0){        
        returnValue = minutes + '分钟前';    
    }else if (seconds > 0){        
        returnValue = seconds + '秒钟前';    
    }    
    return returnValue;
}

判断是否为闰年

function leapYear(year) {    
    return !(year % (year % 100 ? 4 : 400))
}

判断日期是否为今日

/**
*@param {string} val 需要验证的日期  例:2020-01-01
*/
function isToday(val) {            
    return new Date().toLocaleDateString() == new Date(val).toLocaleDateString()       
}