Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to datetime Using C#

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

like image 616
Ashish Avatar asked Jan 01 '10 19:01

Ashish


People also ask

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.

What is ParseExact C#?

ParseExact(String, String, IFormatProvider) Converts the specified string representation of a date and time to its DateTime equivalent using the specified format and culture-specific format information. The format of the string representation must match the specified format exactly.


3 Answers

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

Modify as you want to modify.

like image 64
sikender Avatar answered Nov 12 '22 12:11

sikender


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

Modify as needed.

like image 20
helium Avatar answered Nov 12 '22 12:11

helium


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);
like image 9
JCasso Avatar answered Nov 12 '22 11:11

JCasso