Jui Test Jui Test - 17 days ago 8
ASP.NET (C#) Question

Convert string yyyy/mm/dd/hh/mm to datetime in C#

Hello I want to convert string to DateTime.
My string is like this '2016/11/18/12/03'.2016 is year,11 is month 18 is a day 12 hour and 3 min. I want to convert it into format like this '2016-11-18 12:03:00.000'. How can do that in C#? Can I use split function which form array. That split yyyy, month, date, hh and min. How to add them to form new date?

string s = '2016/11/18/12/03'
string[] arr = s.split('/');

Answer

the below should work -

using System.Globalization;

DateTime result;
CultureInfo provider = CultureInfo.InvariantCulture;
string dateString = "yyyy/MM/dd/HH/mm";
result = DateTime.ParseExact("2016/11/18/12/03", dateString, provider);
//2016-11-18 12:03:00.000
string display = result.ToString("yyyy-MM-dd HH:mm:ss.fff");
Comments