Community discussion forum

How to get the current time of UK from US time using c#

  • 1 year ago

    Hi friends I need a help to get a current time of UK from US current time. Can anybody guide me to solve the above issue Advance thanks for your help [Smile] Thank U

  • 1 year ago

    Hi,

    This should get you started, now sure how accurate it will be, logic dictates that BST/GMT will be a problem, but it should let you convert a timezone.

    Code behind

    Imports Microsoft.Win32

    Partial Class _Default

    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    Dim regTimeZone As RegistryKey = Nothing

    Dim strTimezone As String = ""

    Dim intInteger As Integer = 0

    If Not Page.IsPostBack Then

    regTimeZone = Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones")

    For Each strTimezone In regTimeZone.GetSubKeyNames

    ddlTimeZones.Items.Add(New ListItem(regTimeZone.OpenSubKey(strTimezone).GetValue("Display"), -BitConverter.ToInt32(regTimeZone.OpenSubKey(strTimezone).GetValue("TZI"), 0)))

    Next

    End If

    End Sub

    Protected Sub cmdConvert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdConvert.ClickDim dtmNow As DateTime = New DateTime(Date.Now.Year, Date.Now.Month, Date.Now.Day, txtTime.Text.Split(":")(0), txtTime.Text.Split(":")(1), 0)

    dtmNow = dtmNow.AddMinutes(ddlTimeZones.SelectedValue)

    litResult.Text = dtmNow.ToString("F")

    End Sub

    End Class

    Markup

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >

    <head runat="server">

    <title>Untitled Page</title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div id="div1">

    Time: &nbsp;&nbsp<asp:TextBox runat="server" ID="txtTime" ></asp:TextBox><br />

    Time Zone: &nbsp;&nbsp;<asp:DropDownList runat="server" ID="ddlTimeZones">

    </asp:DropDownList><br />

    <asp:button runat="server" ID="cmdConvert" Text="Convert" /><br />

    Result: &nbsp;&nbsp;<asp:Literal runat="server" ID="litResult" ></asp:Literal>

    </div>

    </form>

    </body>

    </html>

    Give it a try.

    Regards

    Simon C

Post a reply

Enter your message below

Sign in or Join us (it's free).

We'd love to hear what you think! Submit ideas or give us feedback