This question has already been answered:

There is a QString string with the starting date 2014.08.16 10:14:33 , then there is a list of three dates (there may be more), for example:

  1. 2014.08.17 17:28:53
  2. 2014.08.16 10:27:37
  3. 2014.08.15 12:33:17

How to find the closest date from this list to the start date? Those. in the end, it should turn out: the closest date to the original is the line 2014.08.16 10:27:37 . As I understand it, you need to somehow translate strings in QDateTime and then compare them with the initial date, which is closer to the initial one, which we derive. What will this algorithm look like?

Reported as a duplicate by andreycha participants, user207618, Ivan Pshenitsyn , aleksandr barakin , Streletz Aug 20 '16 at 10:11 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

  • @andreycha languages ​​are different. - αλεχολυτ
  • @alexolut and the algorithm is the same - they ask about it. - andreycha
  • @andreycha then all language tags should be removed. - αλεχολυτ

1 answer 1

  1. You can use QDateTime::fromString with the format "yyyy.MM.dd HH:mm:ss" to convert a string to a date.

  2. When all strings are converted, the distance of one date from another can be checked through QDateTime::secsTo , wrapped in std::abs .

  3. Print the one that will give a smaller number.