您好,欢迎访问一九零五行业门户网

javascript 日期时间 转换的方法_javascript技巧

使用的微软的json序列化库那么date会是
复制代码 代码如下:
/date(1224043200000)/
有效的做法是  mydate.replace(/\/date\((.*?)\)\//gi, new date($1));
如果要转换格式,最好写个扩展方法:
date.prototype.pattern=function(fmt) {        
    var o = {        
    m+ : this.getmonth()+1, //月份        
    d+ : this.getdate(), //日        
    h+ : this.gethours()%12 == 0 ? 12 : this.gethours()%12, //小时        
    h+ : this.gethours(), //小时        
    m+ : this.getminutes(), //分        
    s+ : this.getseconds(), //秒        
    q+ : math.floor((this.getmonth()+3)/3), //季度        
    s : this.getmilliseconds() //毫秒        
    };        
    var week = {        
    0 : /u65e5,        
    1 : /u4e00,        
    2 : /u4e8c,        
    3 : /u4e09,        
    4 : /u56db,        
    5 : /u4e94,        
    6 : /u516d      
    };        
    if(/(y+)/.test(fmt)){        
        fmt=fmt.replace(regexp.$1, (this.getfullyear()+).substr(4 - regexp.$1.length));        
    }        
    if(/(e+)/.test(fmt)){        
        fmt=fmt.replace(regexp.$1, ((regexp.$1.length>1) ? (regexp.$1.length>2 ? /u661f/u671f : /u5468) : )+week[this.getday()+]);        
    }        
    for(var k in o){        
        if(new regexp((+ k +)).test(fmt)){        
            fmt = fmt.replace(regexp.$1, (regexp.$1.length==1) ? (o[k]) : ((00+ o[k]).substr((+ o[k]).length)));        
        }        
    }        
    return fmt;        
}
转换后如下:
var ndate=mydate.replace(/\/date\((.*?)\)\//gi, new date($1));
ndate.pattern(yyyy-mm-dd hh:mm);
其它类似信息

推荐信息