I have this date format:
Thu Apr 18, 2019 12:44 pm
I am reading this information from a file... When it finds the first word in this case Thu, it enters if
, and I start collecting the information at the end I have a variable tacTime
where I combine the variables, leaving as a result
18 Apr 2019
-- Is there some type of conversion that in the end obtains the date in this way:
04/18/2018 (mes/dia/año)
--
if (arreglo[0] == "Thu")
{
string m = arreglo[1].Substring(0, 3);
string month = m;
string d = arreglo[2].Substring(0, 2);
string day = d;
string y = arreglo[3].Substring(0, 4);
string year = y;
tacTime = day + " " + month + " " + year;
}
You can do it by applying the direct format with the function
toString
Example:
another serious option
In this case, as Juan Carlos has put in his answer, you can simply use it
DateTime.Parse
to parse your date string to a DateTime.I add another option where you can define exactly the format of your date string to parse it to
DateTime
in case itParse
doesn't correctly detect the date from the string.To do this, you must either use DateTime.ParseExact or DateTime.TryParseExact using the date formats explained in Custom date and time format strings .
In your case, it would be something like this:
And later, if you want a string with another format, as they tell you in the other answer, you can use it
ToString
with a custom string: