Convert string to Datetime by TryParseExact

How do I convert a string to a DateTime using TryParseExact?

For example:

DateTime date;
DateTime.TryParseExact(stringDateTime, "yyyy.MM.dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out date);   

Answers


When you use TryParseExact or ParseExact the input string must be in the format which you are trying to parse it as. So in your example, if stringDateTime is not in the format of yyyy.MM.dd HH:mm:ss then it won't work i.e.

var stringDateTime = "01/01/2013 12:00:00";
DateTime date;
DateTime.TryParseExact(stringDateTime, "yyyy.MM.dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out date); // will return false

stringDateTime = "2013.01.01 20:00:00";
DateTime.TryParseExact(stringDateTime, "yyyy.MM.dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.None, out date); // will return true

Need Your Help

Download Latest Exported File from Directory

php mysql csv download directory

I have a script which is exporting files to a CSV file from a MySQL Database. I am wanting to create a link to download the latest file from the directory. Is there a way to do with PHP?

does WCF's NetTCPBinding supports Socks Proxy?

wcf proxy nettcpbinding

I've got a client/server WCF application I want to deploy in a corporate environement.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.