You can't get the date time form the client PC because the code is executed on the SERVER.
You can use JavaScript to get the current date time from the browser.
After collecting the date time from javascript, set this value in a HiddenField and read on the server and compare with Server DateTime to stop the application from running.
HTML
<asp:HiddenField ID="hfDateTime" runat="server" />
<asp:Button id="btnCheck" Text="" runat="server" OnClick="Check" Style="display: none;" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string script = "$(document).ready(function () { $('#hfDateTime').val((new Date()).toISOString().slice(0, 19).replace(/-/g, '/').replace('T', ' ')); $('#btnCheck').click(); });";
ClientScript.RegisterStartupScript(this.GetType(), "load", script, true);
}
}
protected void Check(object sender, EventArgs e)
{
DateTime client = Convert.ToDateTime(hfDateTime.Value);
DateTime current = GetDateTime();
if (client < current)
{
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Client : " + client.ToString() + "\\nServer : " + current.ToString() + "\\nInvalid System DateTime.');", true);
}
}
public static DateTime GetDateTime()
{
DateTime dateTime = DateTime.MinValue;
System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create("http://www.microsoft.com");
request.Method = "GET";
request.Accept = "text/html, application/xhtml+xml, */*";
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)";
request.ContentType = "application/x-www-form-urlencoded";
request.CachePolicy = new System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore);
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
if (response.StatusCode == System.Net.HttpStatusCode.OK)
{
string todaysDates = response.Headers["date"];
dateTime = DateTime.ParseExact(todaysDates, "ddd, dd MMM yyyy HH:mm:ss 'GMT'",
System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat, System.Globalization.DateTimeStyles.AssumeUniversal);
}
return dateTime;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim script As String = "$(document).ready(function () { $('#hfDateTime').val((new Date()).toISOString().slice(0, 19).replace(/-/g, '/').replace('T', ' ')); $('#btnCheck').click(); });"
ClientScript.RegisterStartupScript(Me.[GetType](), "load", script, True)
End If
End Sub
Protected Sub Check(ByVal sender As Object, ByVal e As EventArgs)
Dim client As DateTime = Convert.ToDateTime(hfDateTime.Value)
Dim current As DateTime = GetDateTime()
If client < current Then
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Client : " & client.ToString() & "\nServer : " + current.ToString() & "\nInvalid System DateTime.');", True)
End If
End Sub
Public Shared Function GetDateTime() As DateTime
Dim dateTime As DateTime = DateTime.MinValue
Dim request As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create("http://www.microsoft.com"), System.Net.HttpWebRequest)
request.Method = "GET"
request.Accept = "text/html, application/xhtml+xml, */*"
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)"
request.ContentType = "application/x-www-form-urlencoded"
request.CachePolicy = New System.Net.Cache.RequestCachePolicy(System.Net.Cache.RequestCacheLevel.NoCacheNoStore)
Dim response As System.Net.HttpWebResponse = CType(request.GetResponse(), System.Net.HttpWebResponse)
If response.StatusCode = System.Net.HttpStatusCode.OK Then
Dim todaysDates As String = response.Headers("date")
dateTime = DateTime.ParseExact(todaysDates, "ddd, dd MMM yyyy HH:mm:ss 'GMT'",
System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat, System.Globalization.DateTimeStyles.AssumeUniversal)
End If
Return dateTime
End Function
Screenshot