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 2


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.

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'
  • This is a comment, not an answer. Feb 5, 2018 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.