Restore ability to customize date format #3929
Replies: 12 comments 14 replies
-
|
Some things I've tried that didn't work:
In all cases the field simply shows 'Invalid DateTime' in the front-end. It seems that the front-end for Nova 4 is built to expect timestamps in specific formats (either That's all fine and dandy, but it's hard for me to accept that Nova 4 has launched without such a basic feature as datetime formatting. I'm getting Nova 2.0 dejavu, where we waited almost 3 years for a fix to a comparable issue - while third parties solved it perfectly - which was finally resolved by adding pickerDisplayFormat() to Nova 3.0 and telling everyone to upgrade. Now that we've paid for another upgrade to 4.0 pickerDisplayFormat() has been removed again... I agree with OP that the picker isn't the most important, but we really should be able to format timestamps on Index and Detail pages. I hope someone from the Nova team can tell us what to realistically expect with regard to this feature so we can all start using custom Datetime fields if need be. |
Beta Was this translation helpful? Give feedback.
-
|
In my opinion, it would be more correct to change this issue back to a bug instead of a feature request since DateTime formatting in the Index and Detail views has been available to us without issues in Nova 3 and it seems a fair expectation for such basic functionality to be maintained, especially since the documentation mentions no such limitations. The text above this Discussion ("bakerkretzmar started this conversation in Ideas & Feature Requests") is not correct, this was reported as a bug in the first place and then turned into an idea/feature request by a maintainer. |
Beta Was this translation helpful? Give feedback.
-
|
@davidhemphill Could you let us know your stance on the issue of formatting DateTime fields on Index and Detail views? It's hard to imagine this being very complicated but maybe we're missing something here. If it's not considered a bug, at least it should be considered a documentation issue because the documentation on Field Resolution/Formatting doesn't seem to apply to DateTime fields (causes 'Invalid DateTime' to appear in the front-end) and the Date Fields documentation doesn't mention the lack of formatting possibilities or how to customize the formatting. This is unpractical, but I also think it's unfair to paying users expecting basic features to survive major version bumps. I would like to know if the Nova team agrees that this should be considered a bug and if there is any indication as to when the feature will be back in Nova 4. Thanks! |
Beta Was this translation helpful? Give feedback.
-
|
I also hope that this is a bug and that the datetime fields should be able to be formatted like all the other fields. The format that it's using at the moment is, to say the least, highly impractical and layout breaking. |
Beta Was this translation helpful? Give feedback.
-
|
We make extensive use of computed fields on the Nova site itself just for this purpose. Text::make('Created At')
->exceptOnForms()
->displayUsing(fn ($d) => $d->diffForHumans());To make this kind of thing a little nicer, // Put this in the boot method of a Service Provider
Field::macro('withFriendlyDate', function () {
return new MergeValue([
$this->onlyOnForms(),
Text::make($this->name)
->exceptOnForms()
->displayUsing(fn ($d) => $d->diffForHumans())
]);
});
// Usage
DateTime::make('Created At')
->filterable()
->withFriendlyDate(), |
Beta Was this translation helpful? Give feedback.
-
|
With regard to the workarounds provided by @davidhemphill in the previous comment; Adding Some things still bit me;
While I understand the elegance of using a MergeValue for auto-showing the actual DateTime field and auto-hiding the computed field on forms, those drawbacks are unacceptable to me so I'd prefer to lose the MergeValue and do the showing/hiding of fields manually in the Resource itself. So, I adapted the macro as follows (I put this within the And then defined my sortable (Don't forget to I now have to define the field twice but that's a small price to pay for customizable DateTime formatting that plays well with localization, sorting and method chaining in the Resource class. I am still groping in the dark as to why we can't simply define how a DateTime field is rendered in the front-end and I insist that having to resort to this macro trickery and doubly defined fields is a workaround, not a true solution, but it is a good workaround that really takes the pressure off needing a native formattable DateTime field. @davidhemphill, you haven't replied to any of the criticisms put forward in this issue and that's fine, but I hope we agree that there is still at the very least a documentation issue -- in my opinion, it shouldn't be this hard to discover how to make a sortable column of formatted DateTime fields in a paid-for admin panel. I hope a real solution comes along someday or that the documentation will be updated to inform other users of how to approach the issue. Having said that, it's completely understandable that Nova 4 is very new and that we have to discover these types of things together. Thanks for putting me in the right direction with the macro suggestion! Update: Filtering the field breaks when not using the MergeValue approach or when using the MergeValue approach and adding the field a second time just for the form views. |
Beta Was this translation helpful? Give feedback.
-
|
So we lost the plain and simple DateTime format method to this? This is a downgrade, is it not? This makes me all the more curious to know the reasons behind this decision. |
Beta Was this translation helpful? Give feedback.
-
|
I've wrapped up this workaround into a separate composer package that also adds 5 custom filters so we can have custom DateTime formats, sortability and filtering at the same time. I think that solves this issue for now. Installation and usage: wdelfuego/nova4-formattable-date Let me know if you run into any problems using that package. |
Beta Was this translation helpful? Give feedback.
-
|
You should be able to use DateTime::make('Created At')->displayUsing(fn ($value) => $value->diffForHumans()), |
Beta Was this translation helpful? Give feedback.
-
|
Newbie here, just learning Laravel and nova after years of other Frameworks. Quite surprised there is no simple way to format a date or date and time, it would seem a very basic feature, even more surprised to learn such a feature used to exist. In fact, I originally googles and found the ->format() method, not realising e solution related to a previous version of Nova, and not until the code threw an exception and I investigated further did I know. Surely such a basic feature must come back? Can we have some feedback from the Nova team please? :wq |
Beta Was this translation helpful? Give feedback.
-
|
Am I right that there is still no reliable way of setting the date format globally once and for all (dates and date pickers) and also no way of setting it per user? Is it possible to get the date format from the user locale with a fallback from Nova::userTimezone(function (Request $request) {
return $request->user()->timezone ?? config('app.timezone');
}); |
Beta Was this translation helpful? Give feedback.
-
|
This seems to be much ado about nothing. The real issue is that there isn't a dedicated Vue component formatting the date (ala Facebook) in the Display view. I have had no issues with formatting, sorting, filtering or other similar matters, using this simple hack. Note: there is probably a better way to detect the request type, but Nova doesn't appear to be exposing it. Date::make('Created')->sortable()->displayUsing(function(Carbon $d) use ($request) {
switch(get_class($request)) {
# We can get Index and Display views from the class name, the others are problematic
case "Laravel\Nova\Http\Requests\ResourceIndexRequest":
return $d->diffForHumans();
case "Laravel\Nova\Http\Requests\ResourceDetailRequest":
# Prints something like: Wednesday 19th of October 2022 08:40:48 AM UTC
return $d->format('l jS \o\f F Y h:i:s A T');
default:
return $d;
}
}),And there is definately a "bug report/issue" component to all of this, because I too am seeing the date in American (MM/DD/YYYY) form, but that appears to be an issue with luxon. > luxon.DateTime.now().toLocaleString({
year: 'numeric',
month: '2-digit',
day: '2-digit',
})
'02/12/2023'I am not American, and the only regional settings on my PC (Windows 10/Chrome) are American English and American Keyboard. The date should most definately be in DD/MM/YYYY form, if it has to be something other than 2023-12-02. This however, appears to be a browser limitation (there doesn't appear to be any supported method for browsers to interpret the date/time regionalisation setting). > Intl.DateTimeFormat().resolvedOptions()
{
"locale": "en-US",
"calendar": "gregory",
"numberingSystem": "latn",
"timeZone": "Australia/Sydney",
"year": "numeric",
"month": "numeric",
"day": "numeric"
}So language choice (I assume) = locale = how you see dates. I would change languages to see if it alters the resultant output -- I'm sure it would -- but that involves a log-out and log-in. |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
Nova 4 removed the ability to use custom date formats for
DateandDateTimefields (docs).For forms that makes sense, but it would be great to still be able to format the displayed date on Index and Detail pages.
Nova 3
Nova 4
Beta Was this translation helpful? Give feedback.
All reactions