I am trying to convert different string date formats to a specific format i.e., YYYYMMDD and all the incoming dates are valid. How can I return a new list of strings representing this format
Asked
Active
Viewed 579 times
-3
-
for dates there's DateTime type in C#. and [here](https://learn.microsoft.com/ru-ru/dotnet/api/system.datetime.tostring?view=net-5.0) you can find how to convert it to string with formatting – ba-a-aton Jun 09 '21 at 12:56
-
2A date 3/4/2000 could be a third of April or a fourth of March. Without additional hints it is impossible to know for sure which is it. – Dialecticus Jun 09 '21 at 12:56
-
Why do you want to convert it to a list of strings? A better choice would be to parse the input list into a list of DateTime instances. Types exist for a reason. – Igor Jun 09 '21 at 12:58
2 Answers
0
Use DateTime.Parse() to get the date/time information into a standard DateTime object and then you can output it any way that you like.
https://learn.microsoft.com/en-us/dotnet/api/system.datetime.parse?view=net-5.0

Matt Small
- 2,182
- 1
- 10
- 16
0
public static List<string> TransformDateFormat(List<string> dates)
{
var formats = new string[]
{
"yyyy/MM/dd",
"dd/MM/yyyy",
"MM-dd-yyyy",
"yyyyMMdd"
};
return dates
.Select(date => DateTime.ParseExact(date, formats, null).ToString("yyyyMMdd"))
.ToList();
}
You must specify all possible formats.

Alexander Petrov
- 13,457
- 2
- 20
- 49