Link to home
Start Free TrialLog in
Avatar of Jimbo99999
Jimbo99999Flag for United States of America

asked on

Vb.Net Date Formatting Assistance

Good Day Experts!

I always have trouble with the data formatting...I hope you can help as I do not understand what I need to do here.  

I have code:
Dim ShipDate As String = ""
ShipDate = rdrNBTYData.Item("SHIP DATE")
ShipDate = FormatDateTime(ShipDate, DateFormat.ShortDate)

So my ShipDate variable has 11/2/2016 in it.  I need to get it in MMddyyyy format with the 0's for when the month and day are single digit.  I have spent hours searching the net and trying just about anything.  Unfortunately I have not been able to achieve the answer of 11022016

Can you help with any suggestions?

Thansk,
jimbo99999
ASKER CERTIFIED SOLUTION
Avatar of Rgonzo1971
Rgonzo1971

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
*NO POINTS*

If it helps, a DateTime object always contains all the information in order to format a parsable object into any supported format.  So given a string that represents a date object in M/d/yyyy format, you can either convert it (as illustrated by Rgonzo) or parse it into a representative DateTime object.  From there, it's a simple matter of outputting the string you want, in this case MMddyyyy.

Proof of concept:
Imports System.Threading
Module Module1
	Sub Main()
		Dim data = "11/2/2016"
		Console.WriteLine("{0} converted to DateTime and output as - {1}", data, Convert.ToDateTime(data).ToString("MMddyyyy"))
		Console.WriteLine("{0} parsed to DateTime and output as - {1}", data, DateTime.Parse(data, Thread.CurrentThread.CurrentCulture).ToString("MMddyyyy"))
		Console.WriteLine("{0} parsed to DateTime (using ParseExact) and output as - {1}", data, DateTime.ParseExact(data, "M/d/yyyy", Thread.CurrentThread.CurrentCulture).ToString("MMddyyyy"))
		Console.ReadLine()
	End Sub
End Module

Open in new window

Produces the following output -User generated image
-saige-
Avatar of Jimbo99999

ASKER

Thanks for replying.  I am back on that project this afternoon.  I will try it next.  Thanks guys.
worked out great.  Thanks again for the help.

jimbo99999