Reputation: 1083
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?
Upvotes: 0
Views: 2442
Reputation: 24125
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.
Upvotes: 1
Reputation: 7027
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.
Upvotes: 3