6

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.

8

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.
1
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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