Date drupal 8:将细枝日期格式设置为以法语输出完整的日期和月份
{{node.field_date_evenement_news.und[0].value2|date(“m/d/Y”,Europe/Paris”)} 输出Date drupal 8:将细枝日期格式设置为以法语输出完整的日期和月份,date,twig,drupal-8,Date,Twig,Drupal 8,{{node.field_date_evenement_news.und[0].value2|date(“m/d/Y”,Europe/Paris”)} 输出 2/22/2016 我希望是这样 22 février 2016 我通过创建自己的细枝过滤器来解决这个问题 您也可以通过创建自己的模块来公开此过滤器 请随意重复使用它 代码 namespace Drupal\twig_extender\TwigExtension; class Dates extends \Twig_Extens
2/22/2016
我希望是这样
22 février 2016
我通过创建自己的细枝过滤器来解决这个问题 您也可以通过创建自己的模块来公开此过滤器 请随意重复使用它 代码
namespace Drupal\twig_extender\TwigExtension;
class Dates extends \Twig_Extension {
/**
* List of all Twig functions
*/
public function getFilters() {
return [
new \Twig_SimpleFilter('date_format', array($this, 'formatDate')),
];
}
/**
* Unique identifier for this Twig extension
*/
public function getName() {
return 'twig_extender.twig.dates';
}
/*
Render a custom date format with Twig
Use the internal helper "format_date" to render the date using the current language for texts
*/
public static function formatDate($date, $format) {
if ($date_format = \DateTime::createFromFormat('Y-m-d', $date)) {
$timestmap = strtotime($date);
}elseif (is_a($date, 'Drupal\Core\Datetime\DrupalDateTime') || is_a($date, 'DateTime')){
$timestmap = $date->getTimestamp();
}else{
$timestmap = $date;
}
return format_date($timestmap, "custom", $format);
}
}
{{ my_unformatted_date|date_format('d M') }}
用法
namespace Drupal\twig_extender\TwigExtension;
class Dates extends \Twig_Extension {
/**
* List of all Twig functions
*/
public function getFilters() {
return [
new \Twig_SimpleFilter('date_format', array($this, 'formatDate')),
];
}
/**
* Unique identifier for this Twig extension
*/
public function getName() {
return 'twig_extender.twig.dates';
}
/*
Render a custom date format with Twig
Use the internal helper "format_date" to render the date using the current language for texts
*/
public static function formatDate($date, $format) {
if ($date_format = \DateTime::createFromFormat('Y-m-d', $date)) {
$timestmap = strtotime($date);
}elseif (is_a($date, 'Drupal\Core\Datetime\DrupalDateTime') || is_a($date, 'DateTime')){
$timestmap = $date->getTimestamp();
}else{
$timestmap = $date;
}
return format_date($timestmap, "custom", $format);
}
}
{{ my_unformatted_date|date_format('d M') }}
这将导致
01 Déc # In French
01 Dec # In English
提示
此筛选器使用多种形式的输入日期,例如:
- 日期时间
- 时间戳
- Drupal\Core\Datetime\DrupalDateTime
希望它能帮助你!只是一个小的更新,格式和日期现在不受欢迎,使用
return\Drupal::service('date.formatter')->format($timestamp,'custom',$format);
相反- 设置我的自定义格式并在/admin/config/regional/date-time上选择语言
- {{date | format_date('my_format_machine_name')}