How to Set local TimeZone in asp.net?

sonasivas

Member
Joined
Apr 11, 2006
Messages
5
Programming Experience
1-3
Hi,
We have created an application in asp.net(in USA) and we have clients in different parts of the world. The problem is , consider we are in eastern timezone (eg:Georgia) and one of our customers is in Texas, now there is a time variation. We came to know from our texas client that the application is displaying our timezone, but not his.
Is there any way to make the application read/take the time from the local system?
Tnx in advance,
Sona
 
ASP.Net runs server-side, to get client time you need to use Javascript and the Date object. How to do it depends a bit on why and when you need the time, for instance if you just need to display it or if you need to retrieve it from client to do server processing at some stage.
 
Hi,
Thankyou for your reply. I'll tell abt one situation in our application. In the application(website), theres a 'new customer entry page'. In that, when a new customer comes to the office and signin, his name and the time of his arrival will be saved in the sql DB. The problem is if a cutomer comes there by 9a.m, in the database its saved as 10a.m(which is Eastern time).

Is there anyway to make some setup in web config file or any other solutions?
 
When any user first load the login page you can capture the local time by Javascript (for instance Body onload event) and put the value in a hidden input field. You see, there have to be a page load from client side before this value can be retreived and some submitting to happen afterwards to transfer it to server. When user then submits credentials you also grab the local time value from that field.
 
Here you got a code example, complete asp.net page including codebehind:
HTML:
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim d As Date = Date.Parse(HiddenField1.Value)
Me.Title = d.ToString
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function gettime() {
var d = new Date();
var h = d.getHours();
var m = d.getMinutes();
var s = d.getSeconds();
if (h<10) {h="0"+h;}
if (m<10) {m="0"+h;}
if (s<10) {s="0"+h;}
document.getElementById("HiddenField1").value = h+":"+m+":"+s;
}
</script>
</head>
<body onload="gettime();">
<form id="form1" runat="server">
<div>
<asp:HiddenField ID="HiddenField1" runat="server" />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" />
</div>
</form>
</body>
</html>
A Button and a HiddenField was added to a page + script. Local time is retrieved from client when page is loaded, when submit button is clicked this value is processed by server into a Date variable and displayed as text in webpage title.
 
Back
Top