Format date by type

Time:2021-3-9

Format date by type

About more daily use of public class operation method, you can pay attention to the next small website http://www.feiaci.com/#/self/…

Format date by type

/**
 *Format date by type
 *@ param {*} date specific date variable
 *@ param {string} datetype needs return types, including ('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 returns the date string in the specified format
 */
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 () + 'year' +( dateObj.getMonth () + 1) +'month '+ dateObj.getDate () + 'Day';
    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: mm: SS){
        dateText =  dateObj.getFullYear () + 'year' + month '+ strdate +' Day '+' + hours + ":" + minutes + ":" + seconds;
    }
    return dateText;
}

There are more date operations: setting the date after a few days, getting the time stamp after n days of the current time, the first day of this week, the last day of this month, week conversion, converting numbers to English, etc. Can be found on the small pulley website( http://www.feiaci.com/#/self/… )Found