Convert string to datetime Using C#

How can i convert String like 20100102 into datetime in a formate of dd/MM/yyyy?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

var userdateformat = DateTime.ParseExact("20101020", "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);

Modify as you want to modify.

Method 2

var result = DateTime.ParseExact("20100102", "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);

Modify as needed.

Method 3

IFormatProvider culture = new CultureInfo("en-EN", false); // use your culture info
DateTime dt = DateTime.ParseExact(myDateTimeString, "yyyyMMdd", culture, DateTimeStyles.NoCurrentDateDefault);

yyyyMMdd is input format here.

And then if you wish convert it to string:

String output = String.Format("{0:dd/MM/yyyy}", dt);

Method 4

You can use DateTime.ParseExact method

Method 5

string strStartDateMain = "20100102";
string strStartDateFinal = new DateTime(Convert.ToInt32strStartDateMain.Substring(0, 4)), Convert.ToInt32(strStartDateMain.Substring(4, 2)), Convert.ToInt32(strStartDateMain.Substring(6))).ToString("dd/MM/yyyy");


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x