Date and Time formats problem(timezones)

Posted on 2007-10-13
Last Modified: 2009-07-29

Im creating a form that lets users enter some information and now i want date stored in this format to a textbox 10/13/2007 currently now it makes it 2007-10-13

One more thing
I want the time to be shown in CEST Central european format (swedish stockholm) format)

Anyone know if its possible?

My code in the add page


<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" title="Untitled Page" %>

<%@ Register Assembly="System.Web.Extensions, Version=, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<script language="javascript" type="text/javascript">
// <!CDATA[

function Image2_onclick() {


function IMG1_onclick() {


// ]]>

           <asp:Label ID="Label1" runat="server" Font-Bold="True" Style="z-index: 100; left: 3px;
                position: absolute; top: 90px" Text="Ban From:"></asp:Label>
            <asp:RadioButtonList ID="radiolist" runat="server" Height="129px" Style="z-index: 101;
                left: 32px; position: absolute; top: 110px" Font-Italic="False">
                <asp:ListItem Value="MTA:VC">MTA:VC</asp:ListItem>
            <asp:CheckBoxList ID="Reasons" runat="server" Style="z-index: 102; left: 5px; position: absolute;
                top: 380px">
                <asp:ListItem Text="Spamming/flooding" Value="1"></asp:ListItem>
                <asp:ListItem Text="Flaming" Value="2"></asp:ListItem>
                <asp:ListItem Text="Constant deathmatching" Value="3"></asp:ListItem>
                <asp:ListItem Text="Teleport hacking" Value="4"></asp:ListItem>
                <asp:ListItem Text="Speed hacking" Value="5"></asp:ListItem>
                <asp:ListItem Text="Weapon hacking" Value="6"></asp:ListItem>
                <asp:ListItem Text="Money cheating" Value="7"></asp:ListItem>
                <asp:ListItem Text="Crashing" Value="8"></asp:ListItem>
                <asp:ListItem Text="Other (Specify in 'Details'field)" Value="9"></asp:ListItem>
            <asp:Label ID="Label2" runat="server" Font-Bold="True" Style="z-index: 103; left: 5px;
                position: absolute; top: 364px" Text="Reason:"></asp:Label>
    &nbsp; &nbsp;&nbsp;
            <asp:Label ID="Label3" runat="server" Font-Bold="True" Style="z-index: 104; left: 5px;
                position: absolute; top: 252px" Text="Player nickname:"></asp:Label>
            <asp:TextBox ID="tbPlayerNickname" runat="server" Style="z-index: 105; left: 5px; position: absolute;
                top: 272px"></asp:TextBox>
            <asp:Label ID="Label4" runat="server" Font-Bold="True" Height="1px" Style="z-index: 106;
                left: 5px; position: absolute; top: 296px" Text="IP address:" Width="101px"></asp:Label>
            <asp:TextBox ID="tbIP" runat="server" Style="z-index: 107; left: 5px; position: absolute;
                top: 315px"></asp:TextBox>
            <asp:CheckBox ID="CheckBox1" runat="server" Style="z-index: 108; left: 8px; position: absolute;
                top: 339px" Text="Dynamic IP" />
            <asp:Label ID="Label6" runat="server" Font-Bold="True" Style="z-index: 109; left: 5px;
                position: absolute; top: 686px" Text="Date:"></asp:Label>
            <asp:TextBox ID="tbDATE" runat="server" Style="z-index: 110; left: 6px; position: absolute;
                top: 705px"></asp:TextBox>
            <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/Calendar_scheduleHS.png"
                Style="z-index: 111; left: 153px; position: absolute; top: 710px" />
    <ajaxToolkit:CalendarExtender ID="CalendarExtender2" runat="server" TargetControlID="tbDATE" PopupButtonID="ImageButton1">
    <asp:Label ID="Label8" runat="server" Style="z-index: 113; left: 44px; position: absolute;
        top: 641px" Text="(CET Server Time / GMT +1)"></asp:Label>
    <asp:Label ID="Label7" runat="server" Font-Bold="True" Height="12px" Style="z-index: 114;
        left: 3px; position: absolute; top: 932px" Text="Banned By"></asp:Label>
    <input id="Reset1" style="z-index: 120; left: 52px; position: absolute; top: 953px"
        type="reset" value="reset" />
            <br />
            <hr style="z-index: 114; left: 0px; position: absolute; top: 0px" />
            &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;
            <br />
            <hr style="z-index: 113; left: 0px; position: absolute; top: 0px" />
            <asp:LoginName ID="LoginName1" runat="server" Style="z-index: 115; left: 80px; position: absolute;
                top: 932px" />
            <hr style="z-index: 115; left: 0px; position: absolute; top: 0px" />
    <img src="banfor/Teamspeak.jpg" style="z-index: 116; left: 3px; width: 28px; position: absolute;
        top: 176px; height: 28px" />
    <img id="IMG1" src="banfor/SAMP.jpg" style="z-index: 117; left: 3px; width: 28px;
        position: absolute; top: 110px; height: 28px" onclick="return IMG1_onclick()" />
    <img src="banfor/MTAVC.jpg" style="z-index: 118; left: 3px; width: 28px; position: absolute;
        top: 143px; height: 28px" />
    <img src="banfor/Forum1.jpg" style="z-index: 119; left: 3px; position: absolute;
        top: 210px" />
            <br />
               <asp:Button ID="btnDoInsert" runat="server" Text="Add" style="z-index: 116; left: 2px; position: absolute; top: 953px" />
            <asp:Label ID="labelDetails" runat="server" Font-Bold="True" Style="z-index: 117;
                left: 5px; position: absolute; top: 599px" Text="Details:"></asp:Label>
            <asp:TextBox ID="tbDetails" runat="server" Style="z-index: 118; left: 5px; position: absolute;
                top: 618px"></asp:TextBox>
    <asp:Label ID="Label5" runat="server" Font-Bold="True" Height="19px" Style="z-index: 120;
        left: 5px; position: absolute; top: 639px" Text="Time:"></asp:Label>
    <asp:Label ID="Label9" runat="server" ForeColor="#FF0000" Height="11px" Style="z-index: 121;
        left: 114px; position: absolute; top: 952px" Width="155px"></asp:Label>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
<asp:TextBox style="Z-INDEX: 102; LEFT: 6px; POSITION: absolute; TOP: 662px" id="txtTime" runat="server" OnTextChanged="txtTime_TextChanged1"></asp:TextBox> <asp:CheckBox style="Z-INDEX: 100; LEFT: 153px; POSITION: absolute; TOP: 661px" id="CheckBox2" runat="server" Text="Time now" Height="19px" Width="110px" Checked="True"></asp:CheckBox>


Vb code
Partial Class _Default
    Inherits System.Web.UI.Page
    Protected Sub btnDoInsert_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDoInsert.Click
        Dim DynIP As String
        If CheckBox1.Checked Then
            DynIP = "Yes"
            DynIP = "No"

        End If
        Dim usrname As String
        usrname = User.Identity.Name

        Dim li As System.Web.UI.WebControls.ListItem
        li = radiolist.SelectedItem

        Dim selectedBanfor As String

        selectedBanfor = li.Text

        Dim selectedReason As String = String.Empty

        Dim IsSelectionsMade As Boolean = False

        For Each tmpListItem As ListItem In Reasons.Items
            If (tmpListItem.Selected) Then
                selectedReason += tmpListItem.Text + ", "
                IsSelectionsMade = True
            End If

        If (IsSelectionsMade) Then
            selectedReason = selectedReason.Substring(0, selectedReason.Length - 2)

            DoInsert(selectedBanfor, tbPlayerNickname.Text, tbIP.Text, DynIP, selectedReason, tbDATE.Text, txtTime.Text, tbDetails.Text, usrname, False)

        End If

    End Sub
    Private Sub DoInsert(ByVal BanFor As String, ByVal PlayerNickName As String, ByVal IPAddress As String, ByVal DynIP As String, ByVal Reasons As String, ByVal tbDATE As String, ByVal Time As String, ByVal Details As String, ByVal BannedBy As String, ByVal CaseClosed As Boolean)

        'ConnectionString till databasen, ändra sökvägen så att den blir rätt
        Dim ConnectionString As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=" + Server.MapPath("~/App_Data/Database.mdf") + ";Integrated Security=True;User Instance=True"

        'Skapa en ny SQL-connection med rätt ConnectionSring
        Dim CurrentSQLConnection As System.Data.SqlClient.SqlConnection = New System.Data.SqlClient.SqlConnection(ConnectionString)

        'Skapa kommando-texten som sätter in en rad i tabellen
        Dim SQLCommand As String = "INSERT INTO Argobanlist (BanFor, PlayerNickName, IPAddress, DynamicIP, Reasons, Date, Time , Details, BannedBy, CaseClosed) "

        SQLCommand += " VALUES ('" + BanFor + "', "
        SQLCommand += "'" + PlayerNickName + "', "
        SQLCommand += "'" + IPAddress + "', "
        SQLCommand += "'" + DynIP + "', "
        SQLCommand += "'" + Reasons + "', "
        SQLCommand += "'" + tbDATE + "', "
        SQLCommand += "'" + Time + "', "
        SQLCommand += "'" + Details + "', "
        SQLCommand += "'" + BannedBy + "', "
        SQLCommand += "" + System.Convert.ToInt32(CaseClosed).ToString() + ")"

        'Öppna anslutning till databasen

        'Skapa nytt SQL-kommando
        Dim sqlCmd_InsertBan As System.Data.SqlClient.SqlCommand = New System.Data.SqlClient.SqlCommand(SQLCommand, CurrentSQLConnection)

        'Utför kommando
        Label9.Text = "Ban added"

        'Stäng anslutningen till databasen igen
    End Sub

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

        If CheckBox2.Checked = False Then
            txtTime.Enabled = True
        End If
        If CheckBox2.Checked = True Then
            txtTime.Enabled = False
            Dim timenow As String
            timenow = DateTime.Now.ToLongTimeString
            txtTime.Text = timenow
        End If
        Dim dtNow As DateTime = DateTime.Now
        tbDATE.Text = dtNow.Date

    End Sub

Hope anyway can help me

If anyone knows about it i would be glad if you can give me the answer in VB code not on C#

Question by:clooak
    LVL 20

    Expert Comment

    Dim d As New Date
    d = d.Now
    Response.Write("<BR>" & Format(d, "MMM/dd/yyyy"))

    LVL 20

    Expert Comment

    sori this one is correct
    Response.Write("<BR>" & Format(d, "MM/dd/yyyy"))
    LVL 20

    Expert Comment

    for timezone you need to use AddHours(+/- time diff)
    here is the result online.

    Response.Write("<BR>Server Date & Time : " & DateTime.Now)
    Response.Write("<BR>Server Date & Time + 2 hrs: " & DateTime.Now.AddHours(2))

    Author Comment

    Still i dont get how i change the date format into a variable etc

    Dim dtNow As DateTime = DateTime.Now
    tbDATE.Text = dtNow.Date
    When i execute the above code i will get the date in 2004-03-17

    I want it to be in this format


    Anyone know how to accomplish that?
    LVL 20

    Expert Comment

    Dim d As New Date
    d = d.Now
    Response.Write("<BR>" & Format(d, "dd/MM/yyyy"))

    it returns dd/mm/yyyy

    your question was formating date as : mm/dd/yyy

    >>Im creating a form that lets users enter some information and now i want date stored in this format to a textbox 10/13/2007 currently now it makes it 2007-10-13


    Author Comment

    Problem solved by myself

    My problem was that i didnt set the Culturel setting to US

    I did that then it worked like a charm!

    Thanks anyway for all help
    LVL 1

    Accepted Solution

    PAQed with points refunded (75)

    EE Admin

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    27 Experts available now in Live!

    Get 1:1 Help Now