Convert string to date format

RIAS
RIAS used Ask the Experts™
on
Convert a string to date format :
dd-mm-yyyy
in vb.net

Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi,

something like:

Dim iDate As String = "dd/mm/yyyy"
Dim oDate As DateTime = Convert.ToDateTime(iDate)
MsgBox(oDate.Day & " " & oDate.Month & "  " & oDate.Year)

Open in new window

Cheers
Senior Developer
Commented:
See Date​Time   Struct: DateTime.Parse() and DateTime.ParseExact().

E.g.

Module Module1
    Sub Main(args As String())

        Dim TheDate As Date

        TheDate = DateTime.Parse("31-12-2019")
        Console.WriteLine(TheDate)

        Console.WriteLine("Done.")
        Console.ReadLine()

    End Sub
End Module

Open in new window

Author

Commented:
ste5an,

The date is in the format 12/09/2019 and need it like 12-09-2019

Thanks
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

ste5anSenior Developer

Commented:
Rephrase your requirements. Cause converting a string to a date is different thing than formatting a date. Especially as a date has no format. A format is applied when when visualizing it. When you don't use an explicit format then the default format provider is used, which uses the settings from your user and the Windows settings.

It's the same as "I want to format &H2 as number." &H2 is a number. It's just formatted or displayed in hexadecimal form.

So what is the actual problem?
dim myString as string = nothing

myString  = Format(myString , "dd-mm-yyyy").ToString
sorry I forgot apiece

dim myString as string = nothing
myString  = Format(CType(myString ,Date) "dd-mm-yyyy")

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial