简体   繁体   中英

Date Time Conversion

I want to convert mm/dd/yyyy to dd/mm/yyyy . My application is asp.NET with VB. I tried following code

DateTime.Parse(oldDate.ToString("dd\mm\yyyy"))

But got the error:

"The string was not recognized as a valid dateTime. There is an unknown word starting at index 2"

Can any one give the appropriate code?

In VB:

Dim dt As DateTime = _
    DateTime.ParseExact(oldDate, "MM/dd/yyyy", CultureInfo.InvariantCulture)

' and then if you want to format it in dd/MM/yyyy format
Dim s As String = dt.ToString("dd/MM/yyyy")

In C#:

DateTime dt =
    DateTime.ParseExact(oldDate, "MM/dd/yyyy", CultureInfo.InvariantCulture);

// and then if you want to format it in dd/MM/yyyy format
string s = dt.ToString("dd/MM/yyyy");

You should escape the \ characters.

if oldDate is a DateTime then all you need to do is

    Dim oldDate As DateTime = DateTime.Now

    Dim odS As String 'old date as string
    odS = oldDate.ToString("ddMMyyyy").Insert(4, "\").Insert(2, "\")

changing the string format does not change the DateTime. DateTime's are numbers, not strings.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM