public static bool TryParse(string value, string format, DateTime currentDate, CultureInfo cultureInfo, bool autoClipTimeParts, out DateTime result) { bool success = false; result = currentDate; if (string.IsNullOrEmpty(value) || string.IsNullOrEmpty(format)) { return(false); } DateTimeParser.UpdateValueFormatForQuotes(ref value, ref format); var dateTimeString = ComputeDateTimeString(value, format, currentDate, cultureInfo, autoClipTimeParts).Trim(); if (!String.IsNullOrEmpty(dateTimeString)) { success = DateTime.TryParse(dateTimeString, cultureInfo.DateTimeFormat, DateTimeStyles.None, out result); } if (!success) { result = currentDate; } return(success); }
private static void UpdateValueFormatForQuotes(ref string value, ref string format) { var quoteStart = format.IndexOf("'"); if (quoteStart > -1) { var quoteEnd = format.IndexOf("'", quoteStart + 1); if (quoteEnd > -1) { var quoteContent = format.Substring(quoteStart + 1, quoteEnd - quoteStart - 1); value = value.Replace(quoteContent, ""); format = format.Remove(quoteStart, quoteEnd - quoteStart + 1); // Use recursive calls for many quote text. DateTimeParser.UpdateValueFormatForQuotes(ref value, ref format); } } }