OammieR
OammieR

Reputation: 2870

How to get server date/time in asp.net

I have to get server date/time when clicking a button but don't know how to get from server.

Upvotes: 2

Views: 14581

Answers (2)

Lion
Lion

Reputation: 19037

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    ' STANDARD TIME example:
    Body.Attributes.Add("onload", "displayDate('" & Format(Now(), "hh:mm:ss tt") & "');")

    ' MILITARY TIME example:
    Body.Attributes.Add("onload", "displayDate('" & Format(Now(), "HH:mm:ss") & "');")
End Sub

I assumed VB as you have not specified a language you're using. you can use it in your button click event with other languages like C# too. Note that my <body> tag has id="Body" and runat="server"

Upvotes: 0

keyboardP
keyboardP

Reputation: 69372

If you're handling the button's click event on the server then you can just call DateTime.Now and it will return the server's time (since the code is executed on the server).

DateTime now = DateTime.Now;

Upvotes: 4

Related Questions