zoukankan      html  css  js  c++  java
  • javascript日期时间倒计时集合

    javascript日期时间倒计时集合

    1  2  3  4

    1.

    <!-- 把如下代码加入<head>区域中 -->
    <script language="JavaScript">
    <!-- //more javascript from http://www.alixixi.com/
    var DifferenceHour = -1
    var DifferenceMinute = -1
    var DifferenceSecond = -1
    //var Tday = new Date("Dec 1, 2012 00:00:01")   var Tday = new Date("12/1/2012 0: 0: 1")        var Tday = new Date("12-1-2012 0: 0: 1")
    var Tday = new Date("12-1-2012 0: 0: 1")
    var daysms = 24 * 60 * 60 * 1000
    var hoursms = 60 * 60 * 1000
    var Secondms = 60 * 1000
    var microsecond = 1000

    function clock()
    {
    var time = new Date()
    var hour = time.getHours()
    var minute = time.getMinutes()
    var second = time.getSeconds()
    var timevalue = ""+((hour > 12) ? hour-12:hour)
    timevalue +=((minute < 10) ? ":0":":")+minute
    timevalue +=((second < 10) ? ":0":":")+second
    timevalue +=((hour >12 ) ? " PM":" AM")
    // document.formnow.now.value = timevalue
    var convertHour = DifferenceHour
    var convertMinute = DifferenceMinute
    var convertSecond = DifferenceSecond
    var Diffms = Tday.getTime() - time.getTime()
    DifferenceHour = Math.floor(Diffms / daysms)
    Diffms -= DifferenceHour * daysms
    DifferenceMinute = Math.floor(Diffms / hoursms)
    Diffms -= DifferenceMinute * hoursms
    DifferenceSecond = Math.floor(Diffms / Secondms)
    Diffms -= DifferenceSecond * Secondms
    var dSecs = Math.floor(Diffms / microsecond)
    if(convertHour != DifferenceHour) document.formnow.dd.value=DifferenceHour
    if(convertMinute != DifferenceMinute) document.formnow.hh.value=DifferenceMinute
    if(convertSecond != DifferenceSecond) document.formnow.mm.value=DifferenceSecond
    document.formnow.ss.value=dSecs
    // document.formnow.Tnow.value= DifferenceHour DifferenceMinute + DifferenceSecond + dSecs
    setTimeout("clock()",1000)
    }
    // end hiding -->

    </script>

    <!-- 修改<body>内容 -->
    <body onload="clock();return true">
    <!-- 把如下代码加入<body>区域中 -->

    <form name="formnow">
    还剩<input type="text" name="dd" size=2> 天
    <input type="text" name="hh" size=2> 小時
    <input type="text" name="mm" size=2> 分
    <input type="text" name="ss" size=2> 秒就到2012年12月1日 00:00:01啦!
    </form>
    说明:<br/>
    各月份中英文对照<br/><br/>

    一月 January<br/>

    二月 February<br/>

    三月 March<br/>

    四月 April<br/>

    五月 May
    <br/>
    六月 Jun<br/>

    七月 July<br/>

    八月 August<br/>

    九月 September<br/>

    十月 October<br/>

    十一月 November<br/>

    十二月 December
    <br />

    ************************************************

    <br />
    2.

    <SCRIPT LANGUAGE="JavaScript" src="js/jquery-min.js"></SCRIPT>
    <DIV ID="RemainD" CLASS=""></DIV>
    <DIV ID="RemainD1" CLASS=""></DIV>
    <SCRIPT LANGUAGE="JavaScript">
    <!--
        var starttime = starttime2 = 0;
        var currentTime = new Date();
        function startServerTime() {

            $.get("http://phpup.net/time.php?random=" + Math.random(), function(data) {
                starttime = starttime2 = data;
            });
        }
        function showServerTime(obj) {
            currentTime.setTime(starttime);
            setTimeout(function() { showServerTime(obj) }, 1);
            $('#' + obj).html(currentTime.getYear() + '年' + (currentTime.getMonth() * 1 + 1) + '月' + currentTime.getDate() + '日' + currentTime.getHours() + ':' + currentTime.getMinutes() + ':' + currentTime.getSeconds() + ':' + currentTime.getMilliseconds()
    + "<br/>");//       

     starttime = starttime * 1 + 16.6;
        }

        function diffTime(lasttime, obj) {
            setTimeout(function() { diffTime(lasttime, obj) }, 1);
            var endtime = new Date(lasttime);
            var nMS = endtime - starttime2;
            var nD = Math.floor(nMS / 86400000);

            var nH = Math.floor((nMS - nD * 86400000) / (3600000));

            var nM = Math.floor((nMS - nD * 86400000 - nH * 3600000) / 60000);

            var nS = Math.floor((nMS - nD * 86400000 - nH * 3600000 - nM * 60000) / 1000);

            var mS = Math.floor(nMS - nD * 86400000 - nH * 3600000 - nM * 60000 - nS * 1000);
            $("#" + obj).html('距' + lasttime + '还有' + nD + '天' + nH + '时' + nM + '分' + nS + '秒' + mS + '毫秒');//
            starttime2 = starttime2 * 1 + 16.6;

     }


     startServerTime();
     diffTime('12/1/2012','RemainD1');
     showServerTime('RemainD');
    //-->
    </SCRIPT><br/>

    ************************************************

    <br />
    3.

    <!--单位:秒-->
    剩余时间:<span id="endtime">10</span>
    <script type="text/javascript">
        var CID = "endtime";
        if (window.CID != null) {
            var iTime = document.getElementById(CID).innerText;
            var Account;
            RemainTime();
        }
        function RemainTime() {
            var iDay, iHour, iMinute, iSecond;
            var sDay = "", sHour = "", sMinute = "", sSecond = "", sTime = "";
            if (iTime >= 0) {
                iDay = parseInt(iTime / 24 / 3600);
                if (iDay > 0) {
                    sDay = iDay + "天";
                }
                iHour = parseInt((iTime / 3600) % 24);
                if (iHour > 0) {
                    sHour = iHour + "小时";
                }
                iMinute = parseInt((iTime / 60) % 60);
                if (iMinute > 0) {
                    sMinute = iMinute + "分钟";
                }
                iSecond = parseInt(iTime % 60);
                if (iSecond >= 0) {
                    sSecond = iSecond + "秒";
                }
                if ((sDay == "") && (sHour == "")) {
                    sTime = "<span style='color:darkorange'>" + sMinute + sSecond + "</font>";
                }
                else {
                    sTime = sDay + sHour + sMinute + sSecond;
                }
                if (iTime == 0) {
                    clearTimeout(Account);
                    sTime = "<span style='color:green'>时间到了!</span>";
                }
                else {
                    Account = setTimeout("RemainTime()", 1000);
                }
                iTime = iTime - 1;
            }
            else {
                sTime = "<span style='color:red'>倒计时结束!</span>";
        }
        document.getElementById(CID).innerHTML = sTime;
    }
    </script><br/>

    *******************************************************************
    <br />

    4.

    <!--将以下代码加入HTML的<Body></Body>之间-->

        <script type="text/javascript">
            function setcountdown(theyear, themonth, theday) {
                yr = theyear; mo = themonth; da = theday
            }

            //设置到期日
            setcountdown(2009, 1, 1);
            var occasion = "2009年1月1日"
           
            var message_on_occasion = "盼望已久的时刻终于来到了!"
           
           
            //样式
            var countdownwidth = '480px'
            var countdownheight = '20px'
            var countdownbgcolor = 'tan'
            var opentags = '<font face="宋体"><small>'
            var closetags = '</small></font>'
           
           
           
           
            //月份
            var montharray = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")
            var crosscount = ''
           
           
            //开始倒计时
            function start_countdown() {
                if (document.layers)
                    document.countdownnsmain.visibility = "show"
                else if (document.all || document.getElementById)//浏览器如果支持document.all或支持document.getElementById
                    crosscount = document.getElementById && !document.all ? document.getElementById("countdownie") : countdownie
                countdown()
            }

            if (document.all || document.getElementById)
                document.write('<span id="countdownie" style="' + countdownwidth + '; background-color:' + countdownbgcolor + '"></span>')

            window.onload = start_countdown

            //倒计时函数
            function countdown() {
                var today = new Date()
               
               
                var todayy = today.getYear()//年
                if (todayy < 1000)
                    todayy += 1900
                var todaym = today.getMonth()//月
                var todayd = today.getDate()//日
                var todayh = today.getHours()//时
                var todaymin = today.getMinutes()//分
                var todaysec = today.getSeconds()//秒


                var todaystring = montharray[todaym] + " " + todayd + ", " + todayy + " " + todayh + ":" + todaymin + ":" + todaysec//今日时间字符串
                futurestring = montharray[mo - 1] + " " + da + ", " + yr//未来时间字符串

  • 相关阅读:
    docker命令
    IntelliJ IDEA设置JVM运行参数
    SpringResourceBundleMessageSource示例(转)
    通过ApplicationContextAware加载Spring上下文环境(转)
    @ControllerAdvice + @ExceptionHandler 全局处理 Controller 层异常(转)
    SpringBoot学习(十一)创建自己的自动配置和Kotlin支持
    SpringBoot学习(十)Spring集成、会话、(监视和管理JMX)、WebSockets和web服务
    BindingResult不能获取错误对象
    SpringBoot学习(九)使用JTA的分布式事务、Hazelcast、Quartz调度器和任务执行和调度
    Maven项目中pom文件分析
  • 原文地址:https://www.cnblogs.com/sgivee/p/1802899.html
Copyright © 2011-2022 走看看