按类型格式化日期

对于更多日常应用的公共类的操作方法,能够关注下小滑轮网站 http://www.feiaci.com/#/self/...

按类型格式化日期

/** * 按类型格式化日期 * @param {*} date 具体日期变量 * @param {string} dateType 须要返回类型,包含('yyyy年mm月dd日','yyyy-mm-dd', *  'yyyy.mm.dd','yyyy-mm-dd MM:mm:ss', 'mm-dd MM:mm:ss', 'yyyy年mm月dd日 MM:mm:ss') * @return {string} dateText 返回为指定格局的日期字符串 */function getFormatDate(date, dateType) {    let dateObj = new Date(date);    let month = dateObj.getMonth() + 1;    let strDate = dateObj.getDate();    let hours = dateObj.getHours();    let minutes = dateObj.getMinutes();    let seconds = dateObj.getSeconds();    if (month >= 1 && month <= 9) {        month = "0" + month;    }    if (strDate >= 0 && strDate <= 9) {        strDate = "0" + strDate;    }    if (hours >= 0 && hours <= 9) {        hours = "0" + hours    }    if (minutes >= 0 && minutes <= 9) {        minutes = "0" + minutes    }    if (seconds >= 0 && seconds <= 9) {        seconds = "0" + seconds    }    let dateText = dateObj.getFullYear() + '年' + (dateObj.getMonth() + 1) + '月' + dateObj.getDate() + '日';    if (dateType == "yyyy-mm-dd") {        dateText = dateObj.getFullYear() + '-' + (dateObj.getMonth() + 1) + '-' + dateObj.getDate();    }    if (dateType == "yyyy.mm.dd") {        dateText = dateObj.getFullYear() + '.' + (dateObj.getMonth() + 1) + '.' + dateObj.getDate();    }    if (dateType == "yyyy-mm-dd MM:mm:ss") {        dateText = dateObj.getFullYear() + '-' + month + '-' + strDate + ' ' + hours + ":" + minutes + ":" + seconds;    }    if (dateType == "mm-dd MM:mm:ss") {        dateText = month + '-' + strDate + ' ' + hours + ":" + minutes + ":" + seconds;    }    if (dateType == "yyyy年mm月dd日 MM:mm:ss") {        dateText = dateObj.getFullYear() + '年' + month + '月' + strDate + '日' + ' ' + hours + ":" + minutes + ":" + seconds;    }    return dateText;}

还有更多的日期操作:设置几天后的日期,获取以后工夫的n天后的工夫戳, 本周第一天, 本月最初一天, 星期转换,将数字转换成英文等等。能够在小滑轮网站(http://www.feiaci.com/#/self/...)找到