Craft 3: Error "variable now does not exist" I upgraded a Craft 2 project to Craft 3 and am working on updating some template quirks. One I am having trouble with involves the following element query:

{% set scriptsAboutToExpire = craft.entries({
    section: 'scripts',
    order: 'expirationDate asc',
    expirationDate: [ 'and', '&gt;=' ~ now, '&lt;=' ~ now|date_modify('+2 weeks') ]
}) %}


This worked great in Craft 2. It grabs all script entries whose expirationDate field is two weeks away from today's date.

However now after upgrading to Craft 3 I am getting the following error when viewing that template:

PHP Recoverable Error – yii\base\ErrorException
Object of class DateTime could not be converted to string

with a highlight on this line:

(isset($context["now"]) || array_key_exists("now", $context) ? $context["now"] : (function () { throw new Twig_Error_Runtime('Variable "now" does not exist.', 10, $this-&gt;source); })())), 2 =&gt; ("&lt;=" . twig_date_modify_filter($this-&gt;env, (isset($context["now"]) || array_key_exists("now", $context) ? $context["now"] : (function () { throw new Twig_Error_Runtime('Variable "now" does not exist.', 10, $this-&gt;source); })()), "+2 weeks"))))), "method");

I tried updating all .localeDate() filters to |date('short') but that had no effect.
You need to convert the DateTime object via Filter:
 now|date('U') 

or via function
now.format('U')

This will format the date as a UNIX timestamp which is sufficient for querying. MySQL datetime format will work too: date('Y-m-d H:i:s')
More info can be found in the upgrade documentation.