Magento 2 – How to Get Current Store Date and Time

datemagento2

In Magento 1.x you could get the store date time via

Mage::getModel('core/date')->gmtDate();

What would be the equivalent to this in Magento 2.x?

Best Answer

You need to inject in your class constructor an instance of \Magento\Framework\Stdlib\DateTime\DateTime and use that one.
Something like this:

protected $date;
public function __construct(
    ....
    \Magento\Framework\Stdlib\DateTime\DateTime $date,
    ....
) {
    ....
    $this->date = $date;
    ....
}

Then, you can use in your class this:

$date = $this->date->gmtDate();