开发者

jquery.countdown in asp.net

Does anyone have experience in using jquery.countdown in asp.net?

In my code behind, I set a date like this

public string datostreng;
protected void Page_Load(object sender, EventArgs e)
{
    st开发者_Go百科ring format = "ddd MMM d yyyy HH:mm:ss";
    DateTime _tempdato = DateTime.Now.AddDays(1);
    datostreng = _tempdato.ToString(format);
}

On the .aspx page, I have

<script type="text/javascript">
    $(function () {
        var dato = new Date();
        dato = '<%=datostreng %>';
        $('#defaultCountdown').countdown({ until: dato, format: 'HMS' });
        //alert(dato);
    });

The counter shows up allright, but it starts counting down from 34 minutes and 56 seconds, weird.. In my example, it should count down from 24 hours.

In the original jquery.countdown sample, they have a dateformat like this: Thu Jan 26 2012 00:00:00 GMT+0100

In my example, it looks like this: fr jan 28 2011 09:50:43

So, I guess the question is, how do I produce a date in C# that satisfies that jquery.countdown function?


When you assign a date to a variable in javascript, you need to use:

var dato = new Date('<%=datostreng%>');

The way you have done it, your variable contains a string, not a date. So just replace those two lines and it should work.


You can take advantage of the fact, that JavaSript holds Date as milliseconds since 1970/01/01. Put this in your code behind:

private static DateTime _jsDateBoundary = new DateTime(1970, 1, 1, 0, 0, 0);
public Int64 GetCountdownMilliseconds()
{
  DateTime countdownDeadline = DateTime.Now.AddDays(1).ToUniversalTime();
  return (Int64)countdownDeadline.Subtract(_jsDateBoundary).TotalMilliseconds;
}

On the .aspx page put this:

$(document).ready(function () {
    $('#defaultCountdown').countdown({ until: new Date(<%= GetCountdownMilliseconds() %>), format: 'HMS' }); 
});

It will work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜