时间函数

function GetLocalTime() {
    var localTime = new Date();
    var time = localTime.getFullYear() + "-" + (parseInt(localTime.getMonth()) + 1) + "-" + localTime.getDate() + "  " + localTime.getHours() + ":" + localTime.getMinutes() + ":" + localTime.getSeconds();

    return time;
};


function SetlocalTime() {
    var time = new Date();
    var y = time.getFullYear();
    var M = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var m = time.getMinutes();
    var s = time.getSeconds();
    var timeStr = y + '-' + (M < 10 ? ('0' + M) : M) + '-' + (d < 10 ? ('0' + d) : d) + "T" + (h < 10 ? ('0' + h) : h) + ":" + (m < 10 ? ('0' + m) : m) + ":" + (s < 10 ? ('0' + s) : s);
    $("#localTime").val(timeStr);


};

var PortalHelp = {
    //注册扩展方法
    FormatTime: function () {
        if (!String.prototype.FormatTime) {
            String.prototype.FormatTime = function () {
                var p = this.length;
                if (p < 2) {
                    return "0" + this.toString();
                }
                return this;
            }
        }
    }
    ,
    //获取COOKIE并得到相差S数
    GetCookie: function () {
        var acctId = document.getElementById("acctid").value;
        var arrCookie = document.cookie.split(";");
        for (var i = 0; i < arrCookie.length; i++) {
            var cookie = arrCookie[i].replace(/[ ]/g, "");;
            if (cookie.indexOf(acctId) != -1) {
                if (cookie.split("=")[0] == acctId) {
                    return cookie.split("=")[cookie.split("=").length - 1]
                }
            }
        }
        return 0;

    }
    ,
    //这是没有缓存版本
    SetBrowserTime: function () {

        var timeSpan = PortalHelp.GetCookie();

        if (isNaN(timeSpan) || timeSpan == "" || timeSpan == null) {
            timeSpan = 1;
        }
        if (!isNaN(timeSpan)) {

            //增加点击事件目的主要为了防止缓存问题
            $("#localTime").trigger("click");

            var loStr = document.getElementById("localTime").value;
            //解决不同浏览器时间格式问题
            loStr = PortalHelp.GetTimeFormat(loStr);

            var locatTime = new Date(loStr);
            var spanTime = parseInt(timeSpan);
            locatTime.setSeconds(locatTime.getSeconds() + spanTime);

            var year = locatTime.getFullYear();
            var month = (parseInt(locatTime.getMonth()) + 1).toString().FormatTime();
            var day = locatTime.getDate().toString().FormatTime();

            var hours = locatTime.getHours().toString().FormatTime();
            var minutes = locatTime.getMinutes().toString().FormatTime();
            var seconds = locatTime.getSeconds().toString().FormatTime();

            var theFirstTime = year + "-" + month + "-" + day;
            var theSecondTime = hours + ":" + minutes + ":" + seconds;
            var hms = hours + ":" + minutes + ":" + seconds;
            var systemTime2 = year + "/" + month + "/" + day + "  " + hms;

            //年度刷新重新刷新页面
            if (location.href.indexOf("Racing") != -1) {
                //if (hms == "24:00:00") {
                //    var href = location.href;
                //    var time = GetLocalTime();
                //    if (href.indexOf("?") != -1) href = href + "&localTime=" + time;
                //    if (href.indexOf("?") == -1) href = href + "?localTime=" + time;
                //    location.href = href;
                //    return;

                //} //00:00:00*/
                if (hms == "00:00:00") location.href = location.href;  //00:00:00
                // if (hms == "18:00:00") location.href = location.href;  //00:00:00
             }

            $("#times").html("<span>&nbsp;</span>" + theSecondTime + "&nbsp;(GMT+8)");
            $("#systemTime").val(theFirstTime + " " + theSecondTime);
            $("#systemTime2").val(systemTime2);
        }


    },
    //这是有缓存版本
    SetBrowserTime2: function (currentYear, currentMonth, currentDay, currentHour, currentMinute, currentSecond) {


        var calcTime = function () {
            if (parseInt(currentSecond) < 59) {
                currentSecond = parseInt(currentSecond) + 1;
            } else {
                currentSecond = "0";
                currentMinute = parseInt(currentMinute) + 1;    //分钟加一,秒钟置零
                if (parseInt(currentMinute) == 60) {
                    currentMinute = "0";
                    currentHour = parseInt(currentHour) + 1;    //小时加一,分钟置零
                    if (parseInt(currentHour) == 24) {
                        currentHour = "0";
                        currentDay = parseInt(currentDay) + 1;      //日期加一,小时置零
                        var tempMonth = parseInt(currentMonth);
                        if (parseInt(tempMonth) == 1 || parseInt(tempMonth) == 3 || parseInt(tempMonth) == 5 || parseInt(tempMonth) == 7 || parseInt(tempMonth) == 8 || parseInt(tempMonth) == 10 || parseInt(tempMonth) == 12) {     //大月
                            if (parseInt(currentDay) > 31) {
                                currentDay = "1";
                                currentMonth = parseInt(currentMonth) + 1;      //月份加一,日期置零
                                if (parseInt(currentMonth) > 12) {
                                    currentMonth = "1";
                                    currentYear = parseInt(currentYear) + 1;        //年份加一,月份置零
                                }
                            }
                        } else {        //小月
                            if (parseInt(currentMonth) == 2) { //二月
                                if ((parseInt(currentYear) % 4 == 0 && parseInt(currentYear) % 100 != 0) || (parseInt(currentYear) % 400 == 0)) { //是润年
                                    if (parseInt(currentDay) > 29) {
                                        currentDay = "1";
                                        currentMonth = parseInt(currentMonth) + 1;
                                    }
                                } else { //非润年
                                    if (parseInt(currentDay) > 28) {
                                        currentDay = "1";
                                        currentMonth = parseInt(currentMonth) + 1;
                                    }
                                }
                            } else {    //非二月
                                if (parseInt(currentDay) > 30) {
                                    currentDay = "1";
                                    currentMonth = parseInt(currentMonth) + 1;
                                }
                            }
                        }
                    }
                }
            }
        }

        setInterval(function () {
            calcTime();
            theFirstTime = currentYear + "-" + (parseInt(currentMonth) < 10 ? ("0" + currentMonth.toString()) : currentMonth.toString()) + "-" + (parseInt(currentDay) < 10 ? ("0" + currentDay.toString()) : currentDay.toString());     //年月日   20130530
            theSecondTime = (parseInt(currentHour) < 10 ? ("0" + currentHour.toString()) : currentHour.toString()) + ":" + (parseInt(currentMinute) < 10 ? ("0" + currentMinute.toString()) : currentMinute.toString()) + ":" + (parseInt(currentSecond) < 10 ? ("0" + currentSecond.toString()) : currentSecond.toString());    //时分秒   15:17:23


            var systemTime2 = currentYear + "/" + (parseInt(currentMonth) < 10 ? ("0" + currentMonth.toString()) : currentMonth.toString()) + "/" + (parseInt(currentDay) < 10 ? ("0" + currentDay.toString()) : currentDay.toString()) + "   " + theSecondTime;
            //$("#time").html(theFirstTime);
            $("#times").html("<span>&nbsp;</span>" + theSecondTime + "&nbsp;(GMT+8)");
            $("#systemTime").val(theFirstTime + " " + theSecondTime);
            $("#systemTime2").val(systemTime2);
        }, 1000);


    }
    ,
    //浏览器类型
    GetBrowserType: function () {

        var OsObject = "";
        if (navigator.userAgent.indexOf("MSIE") > 0) {
            return "MSIE";
        }
        if (isFirefox = navigator.userAgent.indexOf("Firefox") > 0) {
            return "Firefox";
        }
        if (isSafari = navigator.userAgent.indexOf("Safari") > 0) {
            return "Safari";
        }
        if (isCamino = navigator.userAgent.indexOf("Camino") > 0) {
            return "Camino";
        }
        if (isMozilla = navigator.userAgent.indexOf("Gecko") > 0) {
            return "Gecko";
        }
    },
    //时间格式
    GetTimeFormat: function (loStr) {
        var browserType = PortalHelp.GetBrowserType();
        if (browserType != "Firefox" && browserType != "Gecko") {

            loStr = loStr.replace("T", " ");
        }
        if (browserType == "Safari" || browserType == "MSIE") {
            loStr = loStr.replace(/-/g, '/');

        }
        return loStr;
    }
};

 

posted on 2015-08-07 10:48  高达  阅读(160)  评论(0)    收藏  举报

导航