I'm doing lots of conditions like that in entity queries, I haven't encountered a problem yet.
The main thing you have to be aware of is the timezone the data is stored, which is UTC. You need to convert it to the storage timezone (there's a constant for that), or your query will be off by a few hours.
Here's an example based on something I'm doing, if you only store days, then there is a constant for formatting that too.
$date = new DrupalDateTime('21 days ago');
$date->setTimezone(new \DateTimezone(DATETIME_STORAGE_TIMEZONE));
$formatted = $date->format(DATETIME_DATETIME_STORAGE_FORMAT);
$nids = \Drupal::entityQuery('node')
->condition('your_date_field.value', $formatted, '<=')
->range(0, 50)
->execute();
As of Drupal 8.5 change one should use following to get formatted date
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface;
$date = new DrupalDateTime('21 days ago');
$date->setTimezone(new \DateTimezone(DateTimeItemInterface::STORAGE_TIMEZONE));
$formatted = $date->format(DateTimeItemInterface::DATETIME_STORAGE_FORMAT);
>=
work? 20160318 >= 20160218XXXX-XX-XX
(2016-04-18), so it's technically not a number, therefore doing a numerical condition against it wouldn't be valid.'2016-04-18' > '2016-03-18'
works for me, and'2014-04-18' > '2018-02-01'
returnsFALSE
. Yes, it's a string comparison, but given the string format, it should work. After all,'1' < '2'
.'0001' < '2'
, maybe.