码迷,mamicode.com
首页 > 其他好文 > 详细

日期时间化处理

时间:2018-09-28 17:43:38      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:$2   UNC   test   timer   判断   switch   false   diff   reg   

1、判断一个字符串是不是有效时间格式

isDate:function(s)
    {
        var s =s.replace(/-/g,‘/‘);    
        if(typeof o != ‘string‘)return false;
        var da = new Date(s);
        var time = da.getTime();        
        if (isNaN(time)) return false;
        if(time>0&&time.toString().length>=10){
            return time;
        }
        s=s.substr(0, 10).trim();        
        if(!/^(\d{4})\/(\d{1,2})\/(\d{1,2})$/.test(s))return false;
        var year = RegExp.$1-0;
        var month = RegExp.$2-1;
        var date = RegExp.$3-0;
        var obj = new Date(year,month,date);
        return !!(obj.getFullYear()==year && obj.getMonth()==month && obj.getDate()==date);
        
    }

2、时间字符串转换为时间戳

getUnixTime : function(s){
        if(typeof s !=‘number‘)return s;
        if(typeof s !=‘string‘)return 0;
        var s =s.replace(/-/g,‘/‘);
        if(!dateTime.isDate(s))return 0;
        try{ 
            var date =  new Date(s); 
            var t = date.getTime().toString();
            return isNaN(t.substr(0, 10))?0:parseInt(t.substr(0, 10));
            //return isNaN(t)?0:parseInt(t);
        }
        catch(e){
            return 0;
        }
    }

3、时间戳转换为指定格式的日期时间

getLocalTime:function(i,frm){
        if(typeof i !=‘number‘&&typeof i !=‘string‘)return ‘‘;
        try{
            i= isNaN(i)?dateTime.getUnixTime(i)*1000:i;
            if(parseInt(i)==0) return ‘‘;
        }
        catch(e){
            return ‘‘;
        }
        if(i.toString().length<11){
            i=i*1000;
        }
        var date = new Date(i);
        Y = date.getFullYear() + ‘-‘;
        M = (date.getMonth()+1 < 10 ? ‘0‘+(date.getMonth()+1) : date.getMonth()+1) + ‘-‘;
        D = (date.getDate()<10 ? ‘0‘+date.getDate() : date.getDate()) + ‘ ‘;
        h = (date.getHours()<10 ? ‘0‘+date.getHours() : date.getHours()) +  ‘:‘;
        m = (date.getMinutes()<10 ? ‘0‘+date.getMinutes() : date.getMinutes()) +‘:‘;
        s =date.getSeconds()<10 ? ‘0‘+date.getSeconds() : date.getSeconds();        
        var w = {
            "0" : "日",
            "1" : "一", 
            "2" : "二",
            "3" : "三",
            "4" : "四",
            "5" : "五",
            "6" : "六"        
            };
        E=w[date.getDay()];
        var r="";

        switch(frm){
            case "dd":
                   r=D;
                break;
            case "MM-dd HH:mm":
                r=M+D+h+m.replace(":","");
                break;
            case "YY-MM-dd":
                r=Y+M+D.trim();
                break;    
            case "YY-MM":
                r=Y+M.replace("-","");
                break;
            case "YY年MM月dd日":
                r=Y.replace("-","年")+M.replace("-","月")+D.replace(" ","日");
                break;
            case "YY-MM-dd HH:mm":
                r=Y+M+D+h+m.replace(":","");
                break;
            case "MM月dd日":
                r=M.replace("-","月")+D.replace(" ","日");
                break;
            case "HH:mm":
                r=h+m.replace(":","");
                break;
            case "MM-dd HH:mm":
                r=M+D+h+m.replace(":","");
                break;
            case "MM月dd日 E":
                r=M.replace("-","月")+D.replace(" ","日")+" 周"+E;
                break;
            case "YY-MM-dd E":
                r=Y+M+D+" 周"+E;
                break;
            default:
                r=Y+M+D+h+m+s;
                break;
        }
        return r;
    }

3、时间戳计算时间差

DiffTimeRub:function(t){
        if(!t) return‘刚刚‘;
        if(typeof t ==‘string‘) t=dateTime.getUnixTime(t);
        if(t.toString().length == 10){
            t=parseInt(t*1000);
        }
           var n=new Date().getTime(),m = 60*1000,h = 60 * m,d = 24 * h;
           ////console.log(n+"||"+t+"||"+(n - hour));
        if(t > n - m) {
            return "刚刚";
        }
        else if (t > n - h) {
            var diff_minut=parseInt((n - t) / m);
            return diff_minut+"分钟前";
        }
        else if (t > n - d) {
            var diff_hour=parseInt((n - t) / h);
            return diff_hour+"小时前";
        }
        else
        {
            return dateTime.getLocalTime(t,"YY-MM-dd HH:mm");
        }
    }

4、转换星期

getWeekday:function(s){        
        if(typeof s !=‘string‘)return ‘‘;
        if(!dateTime.isDate(s))return ‘‘;
        var w = {         
            "0" : "日",         
            "1" : "一",         
            "2" : "二",         
            "3" : "三",         
            "4" : "四",         
            "5" : "五",         
            "6" : "六"        
            };
        var d=new Date(s).getDay();
        if(d>-1 && d<7)
        {
            console.log(w[d]);
        }
        return w[d];
    }

 

日期时间化处理

标签:$2   UNC   test   timer   判断   switch   false   diff   reg   

原文地址:https://www.cnblogs.com/carry-gan/p/9719566.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!