How To Retrieve Client’s Date in ASP.Net – Easiest way
Friends,
A very short post for a simple task. In this post, we will see how we can get the date of a user browsing the page on server in ASP.Net. DateTime.Now gives us the server Date & Time. Retrieving Client’s date is required in cases when you want to process something based on browser’s date and not Server’s date. To do this, we will use a hidden field and a bit of jQuery.
- In the ASPX page, declare a hidden field. lets name it as hdnClientDate.
- Add the below jQuery that will auto populate the hidden field with the current date when the page is loaded.
- Parse the date in server side on any Postback.
DateTime dd; DateTime.TryParse(hdnClientDate.Value, out dd);
Keep Learning and sharing! Cheers!