Pass DateTime JavaScript value to ASP.NET C#

Pass DateTime JavaScript value to ASP.NET C#

5

Here are some source code to pass DateTime JavaScript value to ASP.NET in C#. It is used to get client side date and time. It is useful for those who need to display client date and time instead of hosting server date and time.

Put these codes at HTML

<input type=”hidden” id=”hdClient” runat=”server” />

Put these codes at ASP.NET code behind

At Page_Load
Page.RegisterClientScriptBlock(“1″,
“<script type=’text/javascript’>
function goforit()
{var now = new Date();
var offset = now.getTimezoneOffset();
document.forms[0].hdClient.value = -offset}
</script>”);

ClientScriptManager manager = Page.ClientScript;
manager.RegisterStartupScript(this.GetType(), “CallSomething”, “goforit();”, true);

And…

Label1.Text =
DateTime.Now.ToUniversalTime().AddMinutes(double.Parse(Request.Form[“hdClient”])).ToString();

Hope this will help. :D

  • http://www.dottiestein.com Inna

    Thanks for the good tips

  • http://www.JayceOoi.com Jayce

    You are most welcome, Inna. :D

  • http://- senthil

    Thanks.

    Very useful for me….

  • http://www.jayceooi.com Jayce

    No problem. ;)

  • Dave

    Thanks for the pointer, Is there a semi-colon missing on the last statement?
    Also, in asp I am having trouble assigning the value. Would i see the value of the hidden input in the value section? furthermore, can you access the value in c# via Label1.Text=hdClient.Value; ?