How can I get node creation date and convert it to custom format in my theme, I tried to make some operations on node date in "MyTheme.theme" file and in preprocess_node
section.
2 Answers
Code for getting node created date.
$date = $variables['node']->getCreatedTime();
// Here you can use drupal's format_date() function, or some custom PHP date formatting.
$variables['date'] = \Drupal::service('date.formatter')->format($date, '$format');// enter date format in $format.
-
Just see this article on Drupal.org's feeds: knackforge.com/blog/rajamohamed/…– flashvnnCommented Mar 25, 2016 at 8:42
Thanks for your answer. If you want to use a custom date format, you have to declare it this way:
$variables['date'] = \Drupal::service('date.formatter')->format($date, 'custom', '$format'); // Where $format is a PHP date string like 'M Y'