I get a certain string variable - it is displayed in the panel element - the text can be stored in it:

  • just
  • 10 seconds ago
  • 39 min backwards
  • 5 h. 45 min. backwards
  • yesterday 23:03
  • 2 days ago
  • November 15, 2017

How can I turn all this beauty into a timer?

What would be not just a static text, but every second / minute / hour / day - the values ​​would change.

  • one
    and maybe in the area where the variable comes from there is a simpler format for describing the time of the event? in theory, the general algorithm is as follows: if we go to the forehead: 1. parse this text, find the value when the event occurred (the most creative section) 2. initialize the timer with this value and the current time - Akubik
  • @Akubik I get these lines when parsing from sites - they don’t have a different view. 1 your option - means to convert these strings to DataTime and further to wise? - Vipz
  • @Vipz Usually on sites when targeting such text PopUp pops up a window with a hollow date, which is much easier to parse than that. And parsit is, well, FIG knows ... - EvgeniyZ
  • and what a site for an example? or the site parser itself is not an option to rewrite? - Akubik
  • @EvgeniyZ - nothing gets out there. Stupidly located in HTML and using JS ticking. And I need this information. - Vipz

1 answer 1

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.