Write a simple parser.
Сейчас: 11/30/2017 2:15:00 PM 30.11.2017 14:15:00 <= только что 30.11.2017 14:14:50 <= 10 секунд назад 30.11.2017 13:36:00 <= 39 мин. назад 30.11.2017 08:30:00 <= 5 ч. 45 мин. назад 29.11.2017 23:03:00 <= вчера 23:03 28.11.2017 14:15:00 <= 2 дня назад 15.11.2017 00:00:00 <= 15 ноября 2017 г. static void Main(string[] args) { Console.OutputEncoding = Encoding.UTF8; args = new[] { "только что", "10 секунд назад", "39 мин. назад", "5 ч. 45 мин. назад", "вчера 23:03", "2 дня назад", "15 ноября 2017 г." }; Console.WriteLine("Сейчас: {0}", DateTime.Now); foreach (String timeStr in args) { DateTime time = Parse(timeStr); Console.WriteLine("{0} <= {1}", time.ToString("dd.MM.yyyy HH:mm:ss"), timeStr); } Console.ReadLine(); } private static DateTime Parse(String timeStr) { if (timeStr == "только что") return DateTime.Now; Int32 curLength = timeStr.Length; // Убираем слово "вчера", если оно присутствует - парсим время timeStr = timeStr.Replace("вчера ", String.Empty); Int32 newLength = timeStr.Length; if (newLength != curLength) { TimeSpan time = TimeSpan.Parse(timeStr); DateTime yesterday = DateTime.Today.AddDays(-1); return yesterday + time; } // Убираем слово "назад", если оно присутствует - это разница во времени timeStr = timeStr.Replace(" назад", String.Empty); newLength = timeStr.Length; if (newLength != curLength) { String[] formats = {@"ss\ \с\е\к\у\н\д", @"mm\ \м\и\н\.", @"h\ \ч\.\ mm\ \м\и\н\.", @"d\ \д\н\я"}; TimeSpan result = TimeSpan.ParseExact(timeStr, formats, CultureInfo.CurrentCulture); return DateTime.Now - result; } else { CultureInfo culture = CultureInfo.CreateSpecificCulture("ru-RU"); String[] formats = {"dd MMMM yyyy г."}; DateTime result = DateTime.ParseExact(timeStr, formats, culture, DateTimeStyles.AssumeLocal); return result; } }
Of course, the formats and culture need to be cached, and not re-created each time. If performance is critical (billions of dates), then instead of implementing on the basis of Replace, you can write your own character parsilka. But, most likely, such a task is not in front of you.
PS You probably will have other formats. For example, s секунд instead of ss секунд . Add them yourself. In TimeSpan do not forget to escape all characters in addition to the format, including spaces.