7

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

11

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
2

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. Commented Feb 5, 2018 at 20:20

Your Answer

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

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