.NET Fiddle

CSharp · Console

/Program.cs

using System;
using System.Globalization;
using System.Linq;
					
// see: https://stackoverflow.com/q/5366285/1016343
public class Program
{
	public static void Main()
	{
		var dtStr = "2011-03-21 13:26";

		var dt1 = dtStr.ToDate("yyyy-MM-dd HH:mm");
		Console.WriteLine(dt1.HasValue ? $"Parsed date: {dt1.Value.ToString()}" : "no valid date");

		var dt2 = dtStr.ToDate(new string[] { "yyyy-MM-dd HH:mm", "M/d/yyyy h:mm:ss tt" });
		Console.WriteLine(dt2.HasValue ? $"Parsed date: {dt2.Value.ToString()}" : "no valid date");

		int? year = dtStr?.ToDate("yyyy-MM-dd HH:mm")?.Year;
		Console.WriteLine(year.HasValue ? $"The year is: {year.Value.ToString()}" : "no valid year");
		
		var patterns=new[] { "dd-MM-yyyy", "dd.MM.yyyy" };
		(new[] { "15-01-2019", "15.01.2019" }).Select(s => s.ToDate(patterns)).Dump(); 
		(new[] { "15-01-2019", "15.01.2019" }).Select(s => s.ToDate("dd-MM-yyyy", "dd.MM.yyyy")).Dump(); 
	}
}

// using System.Globalization;
public static class Extensions
{
	public static DateTime? ToDate(this string dateTimeStr, params string[] dateFmt)
	{
		// example: var dt = "2011-03-21 13:26".ToDate(new string[]{"yyyy-MM-dd HH:mm", 
		//                                                  "M/d/yyyy h:mm:ss tt"});
		const DateTimeStyles style = DateTimeStyles.AllowWhiteSpaces;
		if (dateFmt == null)
		{
			var dateInfo = System.Threading.Thread.CurrentThread.CurrentCulture.DateTimeFormat;
			dateFmt=dateInfo.GetAllDateTimePatterns();
		}
		var result = (DateTime.TryParseExact(dateTimeStr, dateFmt,
	 		CultureInfo.InvariantCulture, style, out var dt)) ? dt : null as DateTime?;
		return result;
	}
}
;