This can be a need of people that they get instant difference between two dates without the postback. In .Net (C# or VB .Net) its pretty much easy and straight but in Javascript its not that much straight (Though is easy).
Anyways here is the solution. Following is the Javascript function calculating date difference in between two dates (give in two textbox named txtStartDate and txtEndDate) and will store the result (in days) in a textbox named txtDuration.
function calculateDuration()
{
var msInDay = 1000 * 60 * 60 * 24; //Milliseconds in a day
var startDateMS = new Date(document.getElementById("txtStartDate").value).getTime();
var endDateMS = new Date(document.getElementById("txtEndDate").value).getTime();
var differenceMS = Math.abs(endDateMS - startDateMS);
document.getElementById("txtDuration").value = Math.round(differenceMS/msInDay);
}
This function can be called like
<asp:TextBox ID="txtDuration" runat="server" onclick="javascript:calculateDuration();" onfocus="javascript:calculateDuration();"></asp:TextBox>
During my life of ASP .Net development I came across some issues which I am trying to bind at one place with the solution for other people.
Auto Ad Code
Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts
Wednesday, October 20, 2010
Thursday, June 24, 2010
Calling a Server side function from Javascript
Many of us needs a server side function to be called from JavaScript. We can do this easily using __doPostBack
as
Javascript Code
function CallServerCode()
{
__doPostBack('btnTest', '');
}
and in HTML
<button type="button" onclick="javascript:CallServerCode();">Call Server Code</button>
<asp:linkbutton id="btnTest" runat="server" text="ASP .Net Button"> </asp:linkbutton>
and the Server side code
Protected Sub btnHidden_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTest.Click
Response.Write("Hello")
End Sub
Now note that whenever the client button with caption "Call Server Code" will be clicked it will call Serverside event of btnTest using Javascript.
Labels:
button,
click,
function,
javascript,
server
Subscribe to:
Posts (Atom)