How to convert date format in vb.net?

90,508

Solution 1

Assuming you want to convert the xml string value to a proper DateTime variable, Net has many methods for this:

' a date value in the string format specified:
Dim xmlDate As String = "07/15/2014 7:07:33 AM"

' create a DATE variable from that string in a known format:
Dim newDate As Date = DateTime.ParseExact(xmlDate, "MM/dd/yyyy h:mm:ss tt",
           Globalization.CultureInfo.InvariantCulture)

Once you have am actual date variable, you can display it in any format required. Doing so does not change the underlying date value, it just changes the output style:

Dim myDt As DateTime = DateTime.Now

Console.WriteLine(mydt.ToString("dd MMM yy HH:mm tt"))
Console.WriteLine(mydt.ToString("MM/dd/yyyy h:mm:ss")) 

DateTime types are a value; they do not have a format. Formats are for how we display data to humans (as with .ToString() above) and how we tell DataTime the pattern to expect when parsing text data from humans into a DateTime variable.

You must be careful when using many of the VB functions. Some do not create date types at all, just new string variables. CDate can be especially problematic when using date strings from other cultures . It assumes the string is in the current culture format, which may not be the case. This can lead to 08/07/yyyy converting to 07/08/yyyy.


From original question:
I am getting xml response date format string is "MM/dd/yyyy h:mm:ss a"

From comment:
xml returning date format is "7/8/2014 12:00:00 PM"

The format specified in the question does not match the example posted in the comment. The xmlDate text is in fact in M/d/yyyy format, not MM/dd/yyyy! Using ParseExact means we are giving DateTime the exact format to expect. When the format does not match the actual string pattern, it will fail:

Dim actualDate As Date
Dim xmlTest As String = "7/8/2014 12:00:00 PM"

actualDate = DateTime.ParseExact(xmlSource, "MM/dd/yyyy h:mm:ss tt",
                                 Globalization.CultureInfo.InvariantCulture)

This will fail because the text is not in MM/dd format. Note that "M/d" can parse dates from strings in the pattern of "MM/dd" because some days and months will be 2 characters ("10/20..."). But the reverse is not true: "MM/dd" will require the leading 0. Specify the correct format and you wont get a format exception:

 actualDate = DateTime.ParseExact(xmlSource, "M/d/yyyy h:mm:ss tt",
                                 Globalization.CultureInfo.InvariantCulture)


ParseExact is probably the best approach here, because it appears you are importing data from elsewhere. For simple data validation of user input, Parse or TryParse are usually enough. These will try to parse the text using any of the format patterns defined for the current culture.

Some cultures have well over 100. This means the user can input date data almost anyway they want and your code can still parse/convert it to a DateTime type.

See DateTime.ParseExact for more information.

Solution 2

Dim theirTime = "07/15/2014 1:43:38 PM"
Dim myFormat = "dd MMM yy HH:mm"

Dim myTime = Format(CDate(theirTime), myFormat)

Solution 3

Dim dat As Date
Dim dd, mm, yyyy As String

DateTimePicker1.Value = DateTimePicker1.Value.AddDays(1)

If Len(DateTimePicker1.Value.Day) = 2 Then
    dd = DateTimePicker1.Value.Day
Else
    dd = "0" & DateTimePicker1.Value.Day
End If

If Len(DateTimePicker1.Value.Month) = 2 Then
    mm = DateTimePicker1.Value.Month
Else
    mm = "0" & DateTimePicker1.Value.Month
End If

yyyy = DateTimePicker1.Value.Year

dat = dd & "/" & mm & "/" & yyyy
Share:
90,508
NarasimhaKolla
Author by

NarasimhaKolla

I work on mobile applications using Java, C#, VB and XML as the graphical front end. Interested in using Web Services for mobile applications

Updated on July 31, 2022

Comments

  • NarasimhaKolla
    NarasimhaKolla almost 2 years

    I am getting xml response date format string is "MM/dd/yyyy h:mm:ss a" but I need convert other date format ""dd MMM yy HH:mm"".

    How to convert date format in vb.net? Please give me any suggestion.

  • Admin
    Admin almost 10 years
    +1 for using ParseExact and InvariantCulture. Well said all around
  • NarasimhaKolla
    NarasimhaKolla almost 10 years
    hi xml returning date format is "7/8/2014 12:00:00 PM" above steps i am folowing its returning errorSystem.FormatException was unhandled by user code HResult=-2146233033 Message=String was not recognized as a valid DateTime. Source=mscorlib
  • Ňɏssa Pøngjǣrdenlarp
    Ňɏssa Pøngjǣrdenlarp almost 10 years
    see edit - you did not correctly describe your problem in the original question, so the code cannot be just cut and pasted. @NarasimhaKolla
  • NarasimhaKolla
    NarasimhaKolla almost 10 years
    hi friend,i edited my question and understood the problem what i made. i will improve my self .Thank you very much for your kind support.issue is clear
  • Philipp Maurer
    Philipp Maurer almost 6 years
    Welcome to Stack Overflow :-) Instead of just posting some lines of code, please always add an explanation of that code to your answer. Otherwise it might be classifed as incomplete and might be removed.
  • Mahmut K.
    Mahmut K. almost 5 years
    Their time format can be "dd/MM/yyyy" or "MM/dd/yyyy". For example "11/12/2019". Is "12", day or month? I think it could be a Problem.