Home  >  Q&A  >  body text

javascript - Recommended library for date formatting?


The project uses moment.js, but after packaging it relies on a lot of localization files. Is there any way to load only Chinese?

Or do you have any recommendations for other lightweight date formatting libraries?

淡淡烟草味淡淡烟草味2680 days ago469

reply all(2)I'll reply

  • 漂亮男人

    漂亮男人2017-05-19 10:15:26

    There you gomoment.js官网下在moment-with-locales.min.jsah, there is only one file

    reply
    0
  • 大家讲道理

    大家讲道理2017-05-19 10:15:26

    I think moment.js is a pretty good library for date format. It encapsulates a lot of functions. If you don’t want to use moment, you can extend Date yourself. The encapsulation method is:

    Date.prototype.format = function(pattern){
        function zeroize(num){
            return num<10?"0"+num:num;
        };
        var pattern = pattern;    //    YYYY-MM-DD 或 MM-DD-YYYY 或 YYYY-MM-DD , hh : mm : ss
        var dateObj = {
            "Y" : this.getFullYear(),
            "M" : zeroize(this.getMonth()+1),
            "D" : zeroize(this.getDate()),
            "h" : zeroize(this.getHours()),
            "m" : zeroize(this.getMinutes()),
            "s" : zeroize(this.getSeconds())
        };
        return pattern.replace(/YYYY|MM|DD|hh|mm|ss/g,function(match){
            switch(match){
                case "YYYY" :
                    return dateObj.Y;
                case "MM" :
                    return dateObj.M;
                case "DD" :
                    return dateObj.D;
                case "hh" :
                    return dateObj.h;
                case "mm" :
                    return dateObj.m;
                case "ss" :
                    return dateObj.s;
            };
        });
    };
    Date.prototype.past = function(pattern,pastDays){
        function zeroize(num){
            return num<10?"0"+num:num;
        };
        var pastday = new Date((this - 0) - 1000*60*60*24*pastDays);
        var pattern = pattern;    //    YYYY-MM-DD 或 MM-DD-YYYY 或 YYYY-MM-DD , hh : mm : ss
        var dateObj = {
            "Y" : pastday.getFullYear(),
            "M" : zeroize(pastday.getMonth()+1),
            "D" : zeroize(pastday.getDate()),
            "h" : zeroize(pastday.getHours()),
            "m" : zeroize(pastday.getMinutes()),
            "s" : zeroize(pastday.getSeconds())
        };
        return pattern.replace(/YYYY|MM|DD|hh|mm|ss/g,function(match){
            switch(match){
                case "YYYY" :
                    return dateObj.Y;
                case "MM" :
                    return dateObj.M;
                case "DD" :
                    return dateObj.D;
                case "hh" :
                    return dateObj.h;
                case "mm" :
                    return dateObj.m;
                case "ss" :
                    return dateObj.s;
            };
        });
    };
    Date.prototype.yestoday = function(pattern){
        return this.past(pattern,1);
    };
    Date.prototype.tomorrow = function(pattern){
        return this.past(pattern,-1);
    }
    Date.prototype.formatDate=function(str){
        str= str.toLowerCase();
        var that=this;
    
        if(/yyyy/.test(str)){
            str=str.replace(/yyyy/,this.getFullYear());
        }
        if(/mm/.test(str)){
            str=str.replace(/mm/,this.getMonth()+1);
        }
        if(/dd/.test(str)){
            str=str.replace(/dd/,this.getDate());
        }
        if(/hh/.test(str)){
            str=str.replace(/hh/,function(){
                return that.getHours()>=10?that.getHours():("0"+that.getHours());
            });
        }
        if(/ii/.test(str)){
            str=str.replace( /ii/,function(){
                return that.getMinutes()>=10?that.getMinutes():("0"+that.getMinutes());
            });
        }
        if(/ss/.test(str)){
            str=str.replace( /ss/,function(){
                return that.getSeconds()>=10?that.getSeconds():("0"+that.getSeconds());
            });
        }
        if(/week/.test(str)){
            if(this.getDay()==0){
                str=str.replace(/week/,'星期日' );
            }
            if(this.getDay()==1){
                str=str.replace(/week/,'星期一' );
            }
            if(this.getDay()==2){
                str=str.replace(/week/,'星期二' );
            }
            if(this.getDay()==3){
                str=str.replace(/week/,'星期三' );
            }
            if(this.getDay()==4){
                str=str.replace(/week/,'星期四' );
            }
            if(this.getDay()==5){
                str=str.replace(/week/,'星期五' );
            }
            if(this.getDay()==6){
                str=str.replace(/week/,'星期六' );
            }
        }
        return str
    }

    Format date: new Date(utc time).format("YYYY-MM-DD")

    reply
    0
  • Cancelreply