How to convert date format to vb.net?

I get the response date format string xml: "MM / dd / yyyy h: mm: ss a", but I need to convert another date format to "dd MMM yy HH: mm" ".

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

+7
datetime
source share
4 answers

Assuming you want to convert the value of an xml string to a valid 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 the actual date variable, you can display it in any format. This does not change the base 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 is a value; they have no format. Formats are how we display data for people (as in .ToString() above) and how we tell the DataTime template to be expected when parsing text data from people into a DateTime variable.

You must be careful when using many of the VB features. 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 is assumed that the string is in the current culture format, which may not be. This may result in converting 08/07/yyyy to 07/08/yyyy .


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

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

The format indicated in the question does not match the example published in the comment. The xmlDate text is actually in M/d/yyyy format, not MM/dd/yyyy ! Using ParseExact means that we give the DateTime format accurate . If the format does not match the actual string pattern, it will not be executed:

 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 not succeed because the text is not in MM/dd format. Please note that "M/d" can parse dates from strings in the "MM/dd" pattern, because within a few days and months there will be two characters ("10/20 ..."). But the converse is not true: "MM / dd" will require a leading 0 . Specify the correct format and you will not 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 seems like you are importing data from other sources. For simple validation of user input data, usually Parse or TryParse . They will try to parse the text using any of the format patterns specific to the current culture.

Some cultures have over 100 . This means that the user can enter date data in almost any case, and your code can still parse / convert it to the DateTime type.

See DateTime.ParseExact for more information.

+12
source share
 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 
+1
source share

You can do it as follows:

 Dim time As DateTime = DateTime.Now 'Your date Dim format As String = "dd MMM yy HH:mm" Dim newdate = time.ToString(format) 
0
source share
 Dim theirTime = "07/15/2014 1:43:38 PM" Dim myFormat = "dd MMM yy HH:mm" Dim myTime = Format(CDate(theirTime), myFormat) 
0
source share

All Articles