轉換Json中的時間戳為標准時間格式


   //出自http://www.cnblogs.com/ahjesus

function ConvertJSONDateToJSDate(jsonDate) {
        ///    <summary>
        ///        json日期格式轉換為正常格式
        ///    </summary>
        ///    <param name="jsonDate" type="String">
        ///        json日期
        ///    </param>
        ///    <returns type="String" />
        try {//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦勞動成果,轉載請注明出處,謝謝!
            var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10));
            var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
            var day = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
            var hours = date.getHours();
            var minutes = date.getMinutes();
            var seconds = date.getSeconds();
            var milliseconds = date.getMilliseconds();
            milliseconds = myPow(10, (3 - milliseconds.toString().length)).toString().substr(1) + milliseconds.toString();
            return date.getFullYear() + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds + "." + milliseconds;
        } catch (ex) {//出自http://www.cnblogs.com/ahjesus 尊重作者辛苦勞動成果,轉載請注明出處,謝謝!
            return "";
        }

        /*
        快速冪計算,傳統計算方式如果冪次是100就要循環100遍求值
        快速冪計算只需要循環7次即可
        求x的y次方 x^y可以做如下分解
        把y轉換為2進制,設第n位的值為i,計算第n位的權為x^(2^(n-1)*i)
        例如2^12
        12的二進制是1100
        12=2^3*1+2^2*1+2^1*0+2^0*0
        因此2^12=2^(2^3+2^2)
        分解得到2^12=2^(2^3)*2^(2^2)
        */
        function myPow(dx, dy) {
            var r = 1;
            while (dy != 0) {
                var b = dy & 1; //取最末尾的一位數,也可以判斷奇偶數,奇數:1,偶數:0
                if (b) {//如果最末尾的數是1,儲存有效值
                    r *= dx;
                }
                dx *= dx; //這里即完成了x^(2^(n-1)*i)的計算
                dy >>= 1; //右位移去掉末尾1位,也可以看成是除以2取整數
            }
            return r;
        }
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM