Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi,

i develop an online system with ASP.NET and VB which is issuing policies for travel insurance. i have a field for "Today" date. this field displays the today date in which the client issuing policies by, i want to get the server Date NOT the client side date because the client might change the date of his device and makes new policies.



ASP.net Vb.NET

how to get the server side date ??





thanks
Posted

If you use
var dt = DateTime.Now;
then you will get the date on the server as that is where the code is run.
 
Share this answer
 
It might be easier to just check the date at the server side after they post. You can then add a response that "The date was not validated by the server. Please check your system date or contact your IS Support."

If you need the date on the client side, I suggest adding the correct date on page load and taking a datediff of that compared to the system date. You can then apply this alteration on post. This doesn't stop the client side system clock being changed after the page is loaded so you will still need to first option.

Lastly, you can call for the server date from javascript using an ajax webservice call. This can be done sync or async so you can get the date on page load and before page submit. I would not advise polling as this can be connection-heavy.

The last alternative is to use signalR which is a very useful async service that can push or pull data from the server. This is much lighter on connections (1 extra per browser) but is most likely massive overkill.

if you need help with your chosen solution then please let me know ^_^

Andy
 
Share this answer
 
Hello,

There are at-least three ways to achieve this.
<oi>
  • Output a JavaScript Block from your ASP.NET Page
    HTML
    <script language="javascript">
    function returnresponse() {
        document.getElementById("timediv").innerText="<%=DateTime.Now %>";
    }
    </script>

  • Create client script dynamically (more[^])
    C#
    void Page_Load(object sender, EventArgs e)
    {
        String scriptText = "";
        scriptText = "var svrDate = '" + DateTime.Now.ToString("dd/MM/yyyy HH:mm:ss") + "';";
        ClientScriptManager.RegisterClientScriptBlock(this.GetType(), "DateTimeScript", scriptText, true);
    }

  • Create a date time service and invoke it using AJAX. (more[^])


Regards,
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900