We help IT Professionals succeed at work.

Convert date format

VBdotnet2005
VBdotnet2005 asked
on
How can I convert a string (date) to this format ("MM/dd/yyyy")?
3/1/2015 or 3/12/2015
Comment
Watch Question

Top Expert 2007

Commented:
myDate = today.tostring("MM/dd/yyyy")

Author

Commented:
Is your sample just convert today to a string format?
I'm not sure what kind of conversion you want to do. Is the input string in a different form, like "March 1, 2015"? Should the output be a string or a date type? Do you need to worry about alternate date formats (European dd/mm/yyyy, etc.)?

You may want to look at the Date.Parse method, depending on exactly what you want to do.
Developer
Top Expert 2015
Commented:
string date = DateTime.Now.ToString("MM/dd/yyyy", System.Globalization.CultureInfo.InvariantCulture);

Open in new window

Kaushal AroraTechnical Analyst

Commented:
Dear VBdotnet2005,

Kindly go through these links:

https://msdn.microsoft.com/en-us/library/w2sa9yss(v=vs.110).aspx
http://net-informations.com/q/faq/stringdate.html
https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx

Hope these help you in solving your problem.

Regards
Kaushal Arora
Most Valuable Expert 2015
Distinguished Expert 2018

Commented:
If your "date" is a string, you need to parse it first:
Dim TxtDate As String
Dim DatDate As Date
Dim MyDate As String

TxtDate = "3/12/2015"

DatDate = DateTime.Parse(TxtDate, System.Globalization.CultureInfo.InvariantCulture)

MyDate = DatDate.ToString("MM'/'dd'/'yyyy")
Console.Writeline(MyDate)

' Or in one line, should you prefer:
MyDate = DateTime.Parse(TxtDate, System.Globalization.CultureInfo.InvariantCulture).ToString("MM'/'dd'/'yyyy")
Console.Writeline(MyDate)

Open in new window

/gustav