Date.parseFunctions['WCF'] = '_parseWCF';
Date._parseWCF = function(input) {
    input = input.replace(
            new RegExp('/Date\\((-?[0-9]+)(?:[a-zA-Z]|(?:\\+|-)[0-9]{4})?\\)/', 'g'), '(new Date($1))');
    return eval(input);
};

 

//wcf json 时间转换问题 safari 和 firefox

var WCFDateParser = {

     // *** RAS Update: RegEx handler for dates ISO and MS AJAX style
    regExDate: function(str,p1, p2,offset,s)
    {
        str = str.substring(1).replace('"','');
        var date = str;
        
        if (str.substring(0,7) == "\\\/Date(") {  // MS Ajax date: /Date(19834141)/       
            str = str.match(/Date\((.*?)\)/)[1];                        
            date = "new Date(" +  parseInt(str) + ")";
        }
        else { // ISO Date 2007-12-31T23:59:59Z                                     
            var matches = str.split( /[-,:,T,Z]/);        
            matches[1] = (parseInt(matches[1],0)-1).toString();                     
            date = "new Date(Date.UTC(" + matches.join(",") + "))";         
       }                  
        return date;
    },
    
    parse: function(text) {
        if (!(!(/[^,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]/.test(
             text.replace(/"(\\.|[^"\\])*"/g, '')))  ))
             throw new Error("Invalid characters in JSON parse string.");                 

        // *** RAS Update:  Fix up Dates: ISO and MS AJAX format support
        var regEx = /(\"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}.*?\")|(\"\\\/Date\(.*?\)\\\/")/g;
        text = text.replace(regEx,this.regExDate);      

        return eval('(' + text + ')');    
    },
    
    parseSafe: function(text) {
        try {return this.parse(text);} catch(e) {return null;}
    }
}

 

 

posted on 2011-11-04 00:16  鱼不爱水  阅读(225)  评论(0编辑  收藏  举报