Hello All,
Hope you are doing good!!
Here i have written a code for getting the browser time zone(comparing with server time zone),
By that i need to print the time zone also beside to time stamp..
Here i am adding the code and screenshot..
- public partial class WebForm1 : System.Web.UI.Page
- {
- public static DateTime getClientTime(string date, object ClientTimeZoneoffset)
- {
- string s1 = "";
- s1 = ClientTimeZoneoffset.ToString().Substring(0, ClientTimeZoneoffset.ToString().IndexOf("#"));
- if (s1 != null)
- {
- string Temp = s1.ToString().Trim();
- if (!Temp.Contains("+") && !Temp.Contains("-"))
- {
- Temp = Temp.Insert(0, "+");
- }
-
- ReadOnlyCollection<TimeZoneInfo> timeZones = TimeZoneInfo.GetSystemTimeZones();
- DateTime startTime = DateTime.Parse(date);
- DateTime _now = DateTime.Parse(date);
- foreach (TimeZoneInfo timeZoneInfo in timeZones)
- {
- string str = Regex.Match(ClientTimeZoneoffset.ToString(), @"\(([^)]*)\)").Groups[1].Value;
- if (timeZoneInfo.ToString().Contains(Temp) && timeZoneInfo.Id.ToString().Contains(str))
- {
- TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById(timeZoneInfo.Id);
- _now = TimeZoneInfo.ConvertTime(startTime, TimeZoneInfo.Utc, tst);
- break;
- }
- }
- return _now;
- }
- else
- return DateTime.Parse(date);
- }
- protected void Page_Load(object sender, EventArgs e)
- {
- if (IsPostBack == true)
- {
- if (String.IsNullOrEmpty(HiddenFieldClientTime.Value) == false)
- {
- string Temp = HiddenFieldClientTime.Value;
-
- TextBox1.Text = getClientTime(DateTime.UtcNow.ToString(), Temp).ToString();
- }
- }
- }
- }
- }
- <script type="text/javascript">
- var minutes;
- function calculate_time_zone() {
- debugger;
- var rightNow = new Date();
- var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0);
- var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0);
- var temp = jan1.toGMTString();
- var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
- temp = june1.toGMTString();
- var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));
- var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);
- var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);
- var dst;
- if (std_time_offset == daylight_time_offset) {
- dst = "0";
- }
- else {
- var hemisphere = std_time_offset - daylight_time_offset;
- if (hemisphere >= 0)
- std_time_offset = daylight_time_offset;
- dst = "1";
- }
- var i;
-
- minutes = convert(std_time_offset);
- TimeField = document.getElementById("HiddenFieldClientTime");
- debugger;
- TimeField.value = minutes + '#' + ' ' + jan1;
- alert('your time zone is ' + jan1);
- }
-
- function convert(value) {
- var hours = parseInt(value);
- value -= parseInt(value);
- value *= 60;
- var mins = parseInt(value);
- value -= parseInt(value);
- value *= 60;
- var secs = parseInt(value);
- var display_hours = hours;
-
- if (hours == 0) {
- display_hours = "00";
- } else if (hours > 0) {
-
- display_hours = (hours < 10) ? "+0" + hours : "+" + hours;
- } else {
-
- display_hours = (hours > -10) ? "-0" + Math.abs(hours) : hours;
- }
- mins = (mins < 10) ? "0" + mins : mins;
- return display_hours + ":" + mins;
- }
-
- onload = calculate_time_zone;
- </script>
- </head>
- <body>
- <form id="form1" runat="server">
- <div>
- <asp:HiddenField ID="HiddenFieldClientTime" runat="server" />
- <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
- <asp:Button ID="Button1" runat="server" Text="Button" />
- </div>
- </form>
- </body>
Can any one guide me here..!
Thank you in advance!!