سؤال

أنا أستخدم ASP.NET 3.5 مع C#. أريد إنشاء مؤقت العد التنازلي ومتطلباتي مثل هذا:

تاريخ نهاية العد التنازلي: 16 يونيو 2010 لذا ، حتى 16 يونيو يأتي الموقت الخاص بي سيظهر الوقت المتذمر.

واسمحوا لي أن أعرف كيفية تحقيق ذلك ، أنا جوجل ، لكنني لم أحصل على حل Excat لمشكلتي.

شكرا مقدما.

هل كانت مفيدة؟

المحلول

هذا شيء تحتاج إلى حله مع JavaScript. الشيء الوحيد الذي عليك القيام به من الخادم هو تعيين تاريخ الانتهاء كمتغير JavaScript. تحتاج إلى JavaScript لأنك فقط تحميل الصفحة من الخادم. بعد ذلك تحتاج إلى التعامل مع العد التنازلي على العميل.

جافا سكريبت

<script type="text/javascript">
    function countdown_clock(clockID, year, month, day, hour, minute) {
        countdown(clockID, year, month, day, hour, minute);
    }

    function countdown(clockID, year, month, day, hour, minute) {
        Today = new Date();
        Todays_Year = Today.getFullYear();
        Todays_Month = Today.getMonth();

        //Convert both today's date and the target date into miliseconds.                           
        Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(),
                             Today.getHours(), Today.getMinutes(), Today.getSeconds())).getTime();
        Target_Date = (new Date(year, month - 1, day, hour, minute, 00)).getTime();

        //Find their difference, and convert that into seconds.                  
        Time_Left = Math.round((Target_Date - Todays_Date) / 1000);

        if (Time_Left < 0)
            Time_Left = 0;

        days = Math.floor(Time_Left / (60 * 60 * 24));
        Time_Left %= (60 * 60 * 24);
        hours = Math.floor(Time_Left / (60 * 60));
        Time_Left %= (60 * 60);
        minutes = Math.floor(Time_Left / 60);
        Time_Left %= 60;
        seconds = Time_Left;

        dps = 's'; hps = 's'; mps = 's'; sps = 's';
        //ps is short for plural suffix.
        if (days == 1) dps = '';
        if (hours == 1) hps = '';
        if (minutes == 1) mps = '';
        if (seconds == 1) sps = '';

        var clock = document.getElementById(clockID);
        clock.innerHTML = days + ' day' + dps + ' ';
        clock.innerHTML += hours + ' hour' + hps + ' ';
        clock.innerHTML += minutes + ' minute' + mps + ' and ';
        clock.innerHTML += seconds + ' second' + sps;

        //Recursive call, keeps the clock ticking.
        setTimeout('countdown("' + clockID + '",' + year + ',' + month + ',' + day + ',' + hour + ',' + minute + ');', 1000);
    }
</script>

ASP.NET

protected override void OnPreRender(EventArgs e)
    {
        DateTime endDate = new DateTime(2010, 6, 1, 0, 0, 0);
        string script = string.Format("countdown_clock('clock', {0}, {1}, {2}, {3}, {4});", endDate.Year, endDate.Month, endDate.Day, endDate.Hour, endDate.Minute);
        ScriptManager.RegisterStartupScript(this, this.GetType(), "countdown", script, true);

        base.OnPreRender(e);
    }

أخذ البرنامج النصي المعدل على سبيل المثال من نصوص بلدي الصغيرة.

نصائح أخرى

إذا كنت ترغب في ذلك بسهولة ، فاستخدم DateTime.

DateTime EventTime = new DateTime(2010, 6, 16);
TimeSpan Duration = EventTime - DateTime.Now;
string TimeTillEvent = Duration.ToString();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top