locked
format label date time in code behind RRS feed

  • Question

  • User855106052 posted

    Hi all!

    I need to format my label LblReservaData to date dd/MM/yyyy format.

    I have the code:

            Con = New OleDbConnection(StrCon)
            Dset = New DataSet()
            Adpt = New OleDbDataAdapter("SELECT * FROM TabReservar WHERE TabReservar.ReservaCod=654", Con)
            Adpt.MissingSchemaAction = MissingSchemaAction.AddWithKey
            Adpt.Fill(Dset, "TodosOsConteudos")
            Drw = Dset.Tables("TodosOsConteudos").Rows.Find("654")
    	LblReservaCod.Text = Drw("ReservaCod").ToString
            LblReservaData.Text = Drw("ReservaData").ToString
            LblReservaHora.Text = Drw("ReservaHora").ToString
            LblReservaIP.Text = Drw("ReservaIP").ToString

    How can I? In this moment the output is: "26/10/2018 00:00:00" and I want only "26/10/2018"

    Thanks...

    Sunday, March 17, 2019 9:56 PM

Answers

  • User-943250815 posted

    First let me correct format string I used. For month "mm" should be "MM"

    LblReservaData.Text = Drw("ReservaData").ToString("dd/MM/yyyy")

    About your error. It should work if Drw("ReservaData") is a Date or DateTime type, as an example just for you make a simple test

    Dim MyDate as DateTime = Now
    Dim D1 as string = MyDate.ToString("dd/MM/yyyy") ' This results in Day/Month/Year
    Dim D2 as string = MyDate.Tostring("yyyy-MM-dd") ' This results in Year-Month-Day

    On the other hand if you already have a string like "26/10/2018 00:00:00" and I want only "26/10/2018"
    You can use substring, or (if you need) convert string to datetime then format with tostring

    Dim MyDate as string = "26/10/2018 00:00:00"
    Dim D1 as string = MyDate.substring(0,10)
    dim D2 as string = DateTime.Parse(MyDate, System.Globalization..CultureInfo.GetCultureInfo("pt-BR")).ToString("MM/dd/YYYY") ' This should result in Month/Day/Year

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 18, 2019 12:18 AM
  • User855106052 posted

    Thank you all.

    I use your suggestions and I made:

    		Dim ReservaData as DateTime = Drw("ReservaData").ToString
    		Dim RD as string = ReservaData.ToString("dd/MM/yyyy")
    		LblReservaData.Text = RD
    		Dim ReservaHora as DateTime = Drw("ReservaHora").ToString
    		Dim RH as string = ReservaHora.ToString("HH:mm")
    		LblReservaHora.Text = RH
    

    Many thanks!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 19, 2019 12:03 AM

All replies

  • User-943250815 posted

    Use

    LblReservaData.Text = Drw("ReservaData").ToString("dd/mm/yyyy")

    Sunday, March 17, 2019 11:15 PM
  • User855106052 posted

    I try but give error:

    Server Error in '/' Application.


    Input string was not in a correct format.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.FormatException: Input string was not in a correct format.

    Source Error:

    Line 56: 		LblReservaEstadoCod.Text = Drw("ReservaEstadoCod").ToString
    Line 57: 		LblReservaCod.Text = Drw("ReservaCod").ToString
    Line 58: 		LblReservaData.Text = Drw("ReservaData").ToString("dd/mm/yyyy")
    Line 59:         LblReservaHora.Text = Drw("ReservaHora").ToString
    Line 60:         LblReservaIP.Text = Drw("ReservaIP").ToString


    Source File:   Line: 58

    Stack Trace:

    [FormatException: Input string was not in a correct format.]
       Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) +213
       Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) +90
    
    [InvalidCastException: Conversion from string "dd/mm/yyyy" to type 'Integer' is not valid.]
       Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) +238
       System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +9872881
       System.Web.UI.Control.OnLoad(EventArgs e) +95
       System.Web.UI.Control.LoadRecursive() +59
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +678
    

     


    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3163.0

    Sunday, March 17, 2019 11:27 PM
  • User-943250815 posted

    First let me correct format string I used. For month "mm" should be "MM"

    LblReservaData.Text = Drw("ReservaData").ToString("dd/MM/yyyy")

    About your error. It should work if Drw("ReservaData") is a Date or DateTime type, as an example just for you make a simple test

    Dim MyDate as DateTime = Now
    Dim D1 as string = MyDate.ToString("dd/MM/yyyy") ' This results in Day/Month/Year
    Dim D2 as string = MyDate.Tostring("yyyy-MM-dd") ' This results in Year-Month-Day

    On the other hand if you already have a string like "26/10/2018 00:00:00" and I want only "26/10/2018"
    You can use substring, or (if you need) convert string to datetime then format with tostring

    Dim MyDate as string = "26/10/2018 00:00:00"
    Dim D1 as string = MyDate.substring(0,10)
    dim D2 as string = DateTime.Parse(MyDate, System.Globalization..CultureInfo.GetCultureInfo("pt-BR")).ToString("MM/dd/YYYY") ' This should result in Month/Day/Year

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, March 18, 2019 12:18 AM
  • User839733648 posted

    Hi indesk,

    You could write like below to ensure the user's local settings don't affect it.

     LblReservaData.Text = Drw("ReservaData").ToString("dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture);

    Reference: https://docs.microsoft.com/en-us/dotnet/api/system.globalization.cultureinfo?view=netframework-4.7.2 

    Best Regards,

    Jenifer

    Monday, March 18, 2019 4:15 AM
  • User-1038772411 posted

    Hii Indesk,

    Try to solve using this,

    LblReervaData.Text=Drw("ReservaData").Tostring("d");

    Monday, March 18, 2019 1:14 PM
  • User855106052 posted

    Thank you all.

    I use your suggestions and I made:

    		Dim ReservaData as DateTime = Drw("ReservaData").ToString
    		Dim RD as string = ReservaData.ToString("dd/MM/yyyy")
    		LblReservaData.Text = RD
    		Dim ReservaHora as DateTime = Drw("ReservaHora").ToString
    		Dim RH as string = ReservaHora.ToString("HH:mm")
    		LblReservaHora.Text = RH
    

    Many thanks!

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 19, 2019 12:03 AM