永远不知道什么时候需要用到的日期格式

1、简单的获取当前日期的指定格式,例如 2016-05-02 12:30

//返回格式类似 2016-05-02
_formatDate: function() {   
     var date = new Date();    
     var y, m, d;   
     y = date.getFullYear();
     m = date.getMonth() + 1; 
     d = date.getDate();
    return     y + '-' + (m>9 ? m : '0'+m) + '-' + (d>9 ? d : '0'+d);
}

//返回格式类似 12:30
_formatTime: function() {
    var date = new Date();
    var H, M, S, time = '';
    H = date.getHours();
    M = date.getMinutes();
    S = date.getSeconds();
    time = (H>9 ? H : '0'+H) + ':' + (M>9 ? M : '0'+M) + ':' + (S>9 ? S : '0'+S);          
    return time;
}

2、简单封装一个根据参数类型设置,返回一个日期格式(date传入日期值,str可以声明是什么类型或者不传,div是指间隔的符号)

//返回格式、
_formatDate: function(date, str, div) {
    if (!date) {
        date = new Date();
    }    else if (typeof date === 'number') {
        date = new Date(date);
    }    else if (typeof date === 'string') {
        date = new Date( /(\d{4})\D?(\d{2})\D?(\d{2})/.exec(date).splice(1).join('/') );
    }
    var y, m, d, H, M, S, time = '';
    if (typeof str === 'string') {
        str = str.toLowerCase();
        if (str === 'datetime') {
            H = date.getHours();
            M = date.getMinutes();
            S = date.getSeconds();
            time = ' ' + (H>9 ? H : '0'+H) + ':' + (M>9 ? M : '0'+M) + ':' + (S>9 ? S : '0'+S);
        }
        else if (/\d(y|m|d)$/i.test(str)) {
            var n = str.substr(0, str.length-1);
            switch (str.substr(-1)) {
                case 'y':
                    date.setFullYear(date.getFullYear() + (+n)); break;
                case 'm':
                    date.setMonth(date.getMonth() + (+n)); break;
                case 'd':
                    date.setDate(date.getDate() + (+n)); break;
            }
        } else {
            div = div || str;
        }
    }
    if (div === undefined) div = '/';
    y = date.getFullYear();
    m = date.getMonth() + 1;
    d = date.getDate();
    return (m>9 ? m : '0'+m) + div + (d>9 ? d : '0'+d) + time;
}

3、一些更简单的获取日期、今天昨天、星期等的格式

//返回当前几月份:本月、几月、XXXX年XX月
_getMonthOfDate:function(dateCup){
    var str = dateCup.replace(/-/g,"/"); 
    var mydate = new Date(str);
    var nowdate = new Date();
    var myYear = mydate.getFullYear(); 
    var myMonth = mydate.getMonth() + 1;
    //不相等说明一定是此去经年
    if (myYear == nowdate.getFullYear()) {
        //不相等说明一定是此去经月
        if (myMonth == (nowdate.getMonth() + 1)) {
            return "本月";
        }else {
            return  myMonth + "月";
        }
    }else {
        return myYear + "年" + myMonth + "月";
    }
},

//返回当前今天、昨天还是周几
_getDayOfDate:function(dateCup){
    var str = dateCup.replace(/-/g,"/");
    var mydate = new Date(str);
    var nowdate = new Date();
    var myYear = mydate.getFullYear();
    var myMonth = mydate.getMonth() + 1;
    var myDay = mydate.getDate();
    //不相等说明一定不是今天
    if (myDay == nowdate.getDate()) {
        return '今天';
    }else if(this._isYesterday(mydate, nowdate)){
        return '昨天';
    }else {
        return this._getWeekendIndex(mydate);
    }
},

//返回几月几号
_getDateOfDate:function(dateCup){
    var str = dateCup.replace(/-/g,"/");
    var mydate = new Date(str);
    var myMonth = mydate.getMonth() + 1;
    var myDay = mydate.getDate();
    return myMonth + "-" + myDay;
},

//返回是否昨天
_isYesterday: function(theday, nowdate) {
    var yesterday = nowdate;
    yesterday.setDate(nowdate.getDate() - 1); 
    return theday.getFullYear() == yesterday.getFullYear() && theday.getMonth() == yesterday.getMonth() && theday.getDate() == yesterday.getDate();
},
//返回是星期几
_getWeekendIndex: function(theday) {
    switch (theday.getDay()) {
        case 0:
            return '周日';
            break;
        case 1:
            return '周一';
            break;
        case 2:
            return '周二';
            break;
        case 3:
            return '周三';
            break;
        case 4:
            return '周四';
            break;
        case 5:
            return '周五';
            break;
        case 6:
            return '周六';
            break;
        default:
            return '周日';
    }
}

4、

你可能感兴趣的:(永远不知道什么时候需要用到的日期格式)