Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert String to Date in .NET if my incoming date format is in YYYYMMDD

Tags:

c#

.net

datetime

What is the best way to convert string to date in C# if my incoming date format is in YYYYMMDD

Ex: 20001106

like image 614
Sreedhar Avatar asked Feb 02 '10 23:02

Sreedhar


People also ask

Which function is used convert string into date format?

Date() Function. as. Date() function in R Language is used to convert a string into date format.


2 Answers

Use DateTime.ParseExact(). Something like:

   string date = "20100102";    DateTime datetime = DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.InvariantCulture); 
like image 92
Brandon Avatar answered Oct 19 '22 23:10

Brandon


 DateTime.TryParseExact(myDateString, "yyyyMMdd",                           CultureInfo.InvariantCulture,                           DateTimeStyles.None, out myDateVar ) 
like image 42
womp Avatar answered Oct 20 '22 00:10

womp