0

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)
{
    string 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?

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.