C#结合JavaScript实现秒杀倒计时的方法
本文实例讲述了C#结合JavaScript实现秒杀倒计时的方法。分享给大家供大家参考。具体如下:
最近做个秒杀活动,要用到倒计时。要求每周三上午10:00开始倒计时
private string Dtime() { byte tempB = (byte)DateTime.Now.DayOfWeek; byte dayByte = (byte)DayOfWeek.Wednesday; DateTime wednesdayNow = DateTime.Now.AddDays(dayByte - tempB); //本星期三 DateTime returnTime = new DateTime (); if (DateTime.Now.DayOfWeek < wednesdayNow.DayOfWeek) { //当前星期小于本星期三 获取本周星期三 returnTime = new DateTime(wednesdayNow.Year,wednesdayNow.Month,wednesdayNow.Day,10,0,0); } else { //当前星期大于本星期三 获取下周星期三 DateTime nextWednesday = wednesdayNow.AddDays(7);//下周三 returnTime = new DateTime(nextWednesday.Year,nextWednesday.Month,nextWednesday.Day,10,0,0); } return returnTime.ToString() }
这个方法可以得到每周三的时间,然后用js对该时间进行倒计时
$(document).ready(function() { $('body').everyTime('1s', function() { var nowDate = new Date(); var endDate = new Date($("#sp_next").html().replace(//-/g, "//")); //这个时间是上个方法返回的时间 var timeold = endDate.getTime() - nowDate.getTime(); var sectimeold = timeold / 1000 var secondsold = Math.floor(sectimeold); var msPerDay = 24 * 60 * 60 * 1000 var e_daysold = timeold / msPerDay var daysold = Math.floor(e_daysold); var e_hrsold = (e_daysold - daysold) * 24 + (daysold * 24); var hrsold = Math.floor(e_hrsold); var e_minsold = (e_hrsold - hrsold) * 60; var minsold = Math.floor((e_hrsold - hrsold) * 60); var seconds = Math.floor((e_minsold - minsold) * 60); if (hrsold > 0 && hrsold < 10) { hrsold = "0" + hrsold; } if (minsold < 10) { minsold = "0" + minsold; } if (seconds < 10) { seconds = "0" + seconds; } if (hrsold < 0) { $(".seckill_time").html("0"); $(".seckill_minute").html("0"); $(".seckill_second").html("0"); } else { $(".seckill_time").html("").html(hrsold); $(".seckill_minute").html("").html(minsold); $(".seckill_second").html("").html(seconds); } }); });
希望本文所述对大家的C#程序设计有所帮助。
赞 (0)