[c#] Convert YYYYMMDD string date to a datetime value

Possible Duplicate:
Convert string to DateTime in c#

A question

I got a string value that actually get from directoryInfo. What i wanted to accomplish is to convert the string value to a date value for making comparison.

The folder name is sample like this C:\FOLD\20111120 and properly another folder path is like this C:\FOLD\20111021

20111120 is actually a date format. I am trying to convert it into date format to made some comparison decide to deleting the whole directory or not.

I think i shall paste my code here

DirectoryInfo dir = new DirectoryInfo(_FolderPath);

foreach (DirectoryInfo f in dir.GetDirectories())
{
     String folderName = f.ToString();
     DateTime ConDt = Convert.ToDateTime(folderName);
     Console.WriteLine(ConDt);
     Console.WriteLine(ConDt.GetType());
   //Console.WriteLine(folderName.GetType());
   //Console.WriteLine(f.GetType());
}

I tried with Convert.toDatetime() and i get error that unable to made the converstion.How can i do so with this?

This question is related to c# .net datetime

The answer is


You should have to use DateTime.TryParseExact.

var newDate = DateTime.ParseExact("20111120", 
                                  "yyyyMMdd", 
                                   CultureInfo.InvariantCulture);

OR

string str = "20111021";
string[] format = {"yyyyMMdd"};
DateTime date;

if (DateTime.TryParseExact(str, 
                           format, 
                           System.Globalization.CultureInfo.InvariantCulture,
                           System.Globalization.DateTimeStyles.None, 
                           out date))
{
     //valid
}

Examples related to c#

How can I convert this one line of ActionScript to C#? Microsoft Advertising SDK doesn't deliverer ads How to use a global array in C#? How to correctly write async method? C# - insert values from file into two arrays Uploading into folder in FTP? Are these methods thread safe? dotnet ef not found in .NET Core 3 HTTP Error 500.30 - ANCM In-Process Start Failure Best way to "push" into C# array

Examples related to .net

You must add a reference to assembly 'netstandard, Version=2.0.0.0 How to use Bootstrap 4 in ASP.NET Core No authenticationScheme was specified, and there was no DefaultChallengeScheme found with default authentification and custom authorization .net Core 2.0 - Package was restored using .NetFramework 4.6.1 instead of target framework .netCore 2.0. The package may not be fully compatible Update .NET web service to use TLS 1.2 EF Core add-migration Build Failed What is the difference between .NET Core and .NET Standard Class Library project types? Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies Nuget connection attempt failed "Unable to load the service index for source" Token based authentication in Web API without any user interface

Examples related to datetime

Comparing two joda DateTime instances How to format DateTime in Flutter , How to get current time in flutter? How do I convert 2018-04-10T04:00:00.000Z string to DateTime? How to get current local date and time in Kotlin Converting unix time into date-time via excel Convert python datetime to timestamp in milliseconds SQL Server date format yyyymmdd Laravel Carbon subtract days from current date Check if date is a valid one Why is ZoneOffset.UTC != ZoneId.of("UTC")?