Mas

Mas

  • NA
  • 473
  • 64.9k

How to get the time zone besides to time stamp

Jan 30 2020 9:51 AM
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..
 
 

  1. public partial class WebForm1 : System.Web.UI.Page  
  2. {  
  3. public static DateTime getClientTime(string date, object ClientTimeZoneoffset)  
  4. {  
  5. string s1 = "";  
  6. s1 = ClientTimeZoneoffset.ToString().Substring(0, ClientTimeZoneoffset.ToString().IndexOf("#"));  
  7. if (s1 != null)  
  8. {  
  9. string Temp = s1.ToString().Trim();  
  10. if (!Temp.Contains("+") && !Temp.Contains("-"))  
  11. {  
  12. Temp = Temp.Insert(0, "+");  
  13. }  
  14. //Retrieve all system time zones available into a collection  
  15. ReadOnlyCollection<TimeZoneInfo> timeZones = TimeZoneInfo.GetSystemTimeZones();  
  16. DateTime startTime = DateTime.Parse(date);  
  17. DateTime _now = DateTime.Parse(date);  
  18. foreach (TimeZoneInfo timeZoneInfo in timeZones)  
  19. {  
  20. string str = Regex.Match(ClientTimeZoneoffset.ToString(), @"\(([^)]*)\)").Groups[1].Value;  
  21. if (timeZoneInfo.ToString().Contains(Temp) && timeZoneInfo.Id.ToString().Contains(str))  
  22. {  
  23. TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById(timeZoneInfo.Id);  
  24. _now = TimeZoneInfo.ConvertTime(startTime, TimeZoneInfo.Utc, tst);  
  25. break;  
  26. }  
  27. }  
  28. return _now;  
  29. }  
  30. else  
  31. return DateTime.Parse(date);  
  32. }  
  33. protected void Page_Load(object sender, EventArgs e)  
  34. {  
  35. if (IsPostBack == true)  
  36. {  
  37. if (String.IsNullOrEmpty(HiddenFieldClientTime.Value) == false)  
  38. {  
  39. string Temp = HiddenFieldClientTime.Value;  
  40. // call the method to convert the utc now time to client time  
  41. TextBox1.Text = getClientTime(DateTime.UtcNow.ToString(), Temp).ToString();  
  42. }  
  43. }  
  44. }  
  45. }  


  1. <script type="text/javascript">  
  2. var minutes;  
  3. function calculate_time_zone() {  
  4. debugger;  
  5. var rightNow = new Date();  
  6. var jan1 = new Date(rightNow.getFullYear(), 0, 1, 0, 0, 0, 0); // jan 1st  
  7. var june1 = new Date(rightNow.getFullYear(), 6, 1, 0, 0, 0, 0); // june 1st  
  8. var temp = jan1.toGMTString();  
  9. var jan2 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));  
  10. temp = june1.toGMTString();  
  11. var june2 = new Date(temp.substring(0, temp.lastIndexOf(" ") - 1));  
  12. var std_time_offset = (jan1 - jan2) / (1000 * 60 * 60);  
  13. var daylight_time_offset = (june1 - june2) / (1000 * 60 * 60);  
  14. var dst;  
  15. if (std_time_offset == daylight_time_offset) {  
  16. dst = "0";  
  17. }  
  18. else {  
  19. var hemisphere = std_time_offset - daylight_time_offset;  
  20. if (hemisphere >= 0)  
  21. std_time_offset = daylight_time_offset;  
  22. dst = "1";  
  23. }  
  24. var i;  
  25. // Here set the value of hidden field to the ClientTimeZone.  
  26. minutes = convert(std_time_offset);  
  27. TimeField = document.getElementById("HiddenFieldClientTime");  
  28. debugger;  
  29. TimeField.value = minutes + '#' + ' ' + jan1;  
  30. alert('your time zone is ' + jan1);  
  31. }  
  32. // This function is to convert the timezoneoffset to Standard format  
  33. function convert(value) {  
  34. var hours = parseInt(value);  
  35. value -= parseInt(value);  
  36. value *= 60;  
  37. var mins = parseInt(value);  
  38. value -= parseInt(value);  
  39. value *= 60;  
  40. var secs = parseInt(value);  
  41. var display_hours = hours;  
  42. // handle GMT case (00:00)  
  43. if (hours == 0) {  
  44. display_hours = "00";  
  45. else if (hours > 0) {  
  46. // add a plus sign and perhaps an extra 0  
  47. display_hours = (hours < 10) ? "+0" + hours : "+" + hours;  
  48. else {  
  49. // add an extra 0 if needed  
  50. display_hours = (hours > -10) ? "-0" + Math.abs(hours) : hours;  
  51. }  
  52. mins = (mins < 10) ? "0" + mins : mins;  
  53. return display_hours + ":" + mins;  
  54. }  
  55. // Adding the funtion to onload event of document object  
  56. onload = calculate_time_zone;  
  57. </script>  
  58. </head>  
  59. <body>  
  60. <form id="form1" runat="server">  
  61. <div>  
  62. <asp:HiddenField ID="HiddenFieldClientTime" runat="server" />  
  63. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  64. <asp:Button ID="Button1" runat="server" Text="Button" />  
  65. </div>  
  66. </form>  
  67. </body> 
Can any one guide me here..!
 
Thank you in advance!!

Answers (1)