Forums › Forums › OroPlatform › OroPlatform – How do I? Questions › How to change date format?
This topic contains 8 replies, has 4 voices, and was last updated by Stepan Yudin 8 years, 3 months ago.
Starting from March 1, 2020 the forum has been switched to the read-only mode. Please head to StackOverflow for support.
- CreatorTopic
- November 10, 2015 at 5:22 am #36121
Hi everyone!
Is there an easy solution to change Date and DateTime format?
In fact, i have to change format without switching the locale in settings. And i didnt found anything that looks like settings for date/datetime formatter in LocaleBundle configuration class.Docker environment for Symfony2/OroCRM/OroBAP development on Windows platform
https://github.com/stepansib/sf-oro-docker - CreatorTopic
- AuthorReplies
- November 10, 2015 at 8:51 am #36122
Hi,
Which format do you need? Are you following this?
November 12, 2015 at 1:56 pm #36123Hi Stepan,
Do you want to make changes in the date/time formatting on the system/org level, or apply them to a specific fields only? Please tell us more about your use cases.
Thank you.
January 14, 2016 at 6:55 am #36124Oro guys help please!I have the same problem, I want change date/time format for all system. Can you help with it.
January 14, 2016 at 11:19 am #36125Sure. Same question for you though: Can you please specify, what format you are trying to change exactly? Normally, the date & time format of an organization depends on its configured locale; please refer to our user guide to learn how to change it.
January 14, 2016 at 6:41 pm #36126Yes, I found solution several month ago, afternoon i will post it here. In summary – i extended Formatter and use new class in formatter service
Docker environment for Symfony2/OroCRM/OroBAP development on Windows platform
https://github.com/stepansib/sf-oro-dockerJanuary 14, 2016 at 10:40 pm #36127Hi Oro guys.I need the format “d.m.Y H:i:s”.
@Stepan, Can you share with me your solution please?January 16, 2016 at 5:55 am #36128Sorry for delay, i was really busy last quarter.
My task was to define only one DateTime format for any locale choosed Oro in settings.
This solution is little dirty but works)0. I created my own LocaleBundle
1. I extended DateTimeFormatter (see parent class and bundle for explanation)
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697<?php/*** Created by PhpStorm.* User: Stepan Yudin* Date: 11.11.2015* Time: 13:29*/namespace Override\LocaleBundle\Formatter;use Oro\Bundle\LocaleBundle\Formatter\DateTimeFormatter;/*** Class OQDateTimeFormatter* This class overrides stanndart Oro datetime formatter, forces locale to null and define custom pattern* @package Override\LocaleBundle\Formatter*/class OQDateTimeFormatter extends DateTimeFormatter{const OQ_DATE_PATTERN = "dd.MM.yyyy";const OQ_TIME_PATTERN = "HH:mm:ss";const OQ_DATETIME_PATTERN = "dd.MM.yyyy в HH:mm:ss";/*** @inheritdoc*/public function formatDate($date, $dateType = null, $locale = null, $timeZone = null){return $this->format($date, $dateType, \IntlDateFormatter::NONE, null, $timeZone, self::OQ_DATE_PATTERN);}/*** @inheritdoc*/public function formatTime($date, $timeType = null, $locale = null, $timeZone = null){return $this->format($date, \IntlDateFormatter::NONE, $timeType, null, $timeZone, self::OQ_TIME_PATTERN);}/*** @inheritdoc*/public function format($date, $dateType = null, $timeType = null, $locale = null, $timeZone = null, $pattern = null){if (!$timeZone) {$timeZone = $this->localeSettings->getTimeZone();}$dateTime = $this->getDateTime($date);if (null === $pattern) {$pattern = self::OQ_DATETIME_PATTERN;}// use Formatter if we have DateTime object and return the incoming argument otherwiseif ($dateTime) {$formatter = $this->getFormatter($dateType, $timeType, null, $timeZone, $pattern);return $formatter->format((int)$dateTime->format('U'));}return $date;}/*** @inheritdoc*/public function getPattern($dateType, $timeType, $locale = null){$locale = null;if (null === $dateType) {$dateType = static::DEFAULT_DATE_TYPE;}if (null === $timeType) {$timeType = static::DEFAULT_TIME_TYPE;}$pattern = self::OQ_DATETIME_PATTERN;if ($dateType == \IntlDateFormatter::NONE) {$pattern = self::OQ_TIME_PATTERN;}if ($timeType == \IntlDateFormatter::NONE) {$pattern = self::OQ_DATE_PATTERN;}$dateType = $this->parseDateType($dateType);$timeType = $this->parseDateType($timeType);$localeFormatter = new \IntlDateFormatter($locale, $dateType, $timeType, null, \IntlDateFormatter::GREGORIAN, $pattern);return $localeFormatter->getPattern();}}2. I replaced DateTimeFormatter class in my bundle services.yml with my new OQDateTimeFormatter class:
1234parameters:oro_locale.formatter.date_time.class: Override\LocaleBundle\Formatter\OQDateTimeFormatterDocker environment for Symfony2/OroCRM/OroBAP development on Windows platform
https://github.com/stepansib/sf-oro-dockerJanuary 16, 2016 at 6:00 am #36129And dont forget to clear all caches and dump all JS (because there are dependencies with moment.js)
Docker environment for Symfony2/OroCRM/OroBAP development on Windows platform
https://github.com/stepansib/sf-oro-docker - AuthorReplies
The forum ‘OroPlatform – How do I? Questions’ is closed to new topics and replies.