Giter Site home page Giter Site logo

nova-datetime's Introduction

This package adds a DateTime field with support for a global DateTime format, syntactic sugar for formatting individual DateTime fields and powerful date filters for Index views. It can serve as a base for more extensions and improvements with regard to DateTime fields and logic in Laravel's Nova 4.

Installation

composer require wdelfuego/nova-datetime

Usage

Formatting DateTime fields globally

  1. First, publish this package's config file by running:

    php artisan vendor:publish --provider="Wdelfuego\Nova\DateTime\ServiceProvider"
  2. Then, set the format you want to use for all of your DateTime fields in config/nova-datetime.php, for example:

    return [
        'globalFormat' => 'Y-M-d H:i:s',
    ];
    
  3. In your Nova resource, replace all instances of Laravel\Nova\Fields\DateTime with instances of Wdelfuego\Nova\DateTime\Fields\DateTime by adding this use statement:

    use Wdelfuego\Nova\DateTime\Fields\DateTime;
    

This allows you to apply the global format to all DateTime fields in your own Nova resources automatically.

To automatically apply the global DateTime format to the 'Action Happened at' column of the action events in your resources' action logs as well, install the wdelfuego/nova-actions package.

Formatting individual DateTime fields

The examples below assume that the Eloquent model used for the Nova resource has an attribute named 'attribute'.

The withDateFormat helper is added automatically to all DateTime fields in your project (including Nova's own, so you don't have to use a custom DateTime field) and allows you to directly set the format you want the field to be displayed in:

    DateTime::make(__('Localized label'), 'attribute')
        ->withDateFormat('d-M-Y, H:i'),

It is simple syntactic sugar around the displayUsing method that works on DateTime fields since Nova 4.2.4.

Filtering resources by DateTime fields

You can use Laravel's native filterable method on your DateTime fields for a standard date range filter or use any combination of the date filters below to give your end users powerful ways to filter their Nova resources from the Index view.

  • OnDate only shows items where the DateTime field matches a specific date
  • NotOnDate only shows items where the DateTime field is not on a specific date
  • AfterDate only shows items where the DateTime field is after a specific date
  • AfterOrOnDate only shows items where the DateTime field is either after or on a specific date
  • NotAfterDate only shows items where the DateTime field is not after a specific date (this is a fully equivalent alias to BeforeOrOnDate)
  • BeforeDate only shows items where the DateTime field is before a specific date
  • BeforeOrOnDate only shows items where the DateTime field is either before or on a specific date
  • NotBeforeDate only shows items where the DateTime field is not before a specific date (this is a fully equivalent alias to AfterOrOnDate)

You can add a combination of these filters to the Nova resource to allow end users to define a date range.

For example, you could make a standard date range filter that allows users to exclude a specific date like this:

use Wdelfuego\Nova\DateTime\Filters\AfterDate;
use Wdelfuego\Nova\DateTime\Filters\BeforeDate;
use Wdelfuego\Nova\DateTime\Filters\NotOnDate;
    public function filters(NovaRequest $request)
    {
        return [
            new AfterDate(__('After'), 'attribute'),
            new BeforeDate(__('Before'), 'attribute'),
            new NotOnDate(__('But not on'), 'attribute'),
        ];
    }

You can also filter for specific dates only using just a single OnDate filter, or force open-ended range filtering by adding just one of the After or Before filters.

Support

For any problems, questions or remarks you might have, please open an issue on GitHub.

nova-datetime's People

Stargazers

Mounir Azizi avatar Piotr avatar  avatar repat avatar Attila Fulop avatar Talk is cheap show me the code  avatar Krot Eval avatar Donovan Hare avatar Jèfferson Gonçalves avatar wdelfuego avatar

Watchers

wdelfuego avatar

nova-datetime's Issues

TimeZone Problem.

Related to issue #1

Unfortunately the implemented solution doesn't follow the nova behavior with timezones.

the solution you implemented

public static function withDateFormatFunction() : callable { return function (string $format) { return $this->displayUsing(fn ($d) => ($d instanceof Carbon) ? (config('app.timezone') ? $d->setTimezone(new DateTimeZone(config('app.timezone')))->translatedFormat($format) : $d->translatedFormat($format)) : (($d instanceof DateTimeInterface) ? (config('app.timezone') ? $d->setTimezone(new DateTimeZone(config('app.timezone')))->format($format) : $d->format($format)) : '') ); }; }

is using config('app.timezone') which is the default timezone of the application. But laravel nova is providing a way to personalise the timezone per user : see => https://nova.laravel.com/docs/4.0/resources/date-fields.html#timezones

use Laravel\Nova\Nova;
use Illuminate\Http\Request;

/**

Bootstrap any application services.

@return void
*/
public function boot()
{
parent::boot();

Nova::userTimezone(function (Request $request) {
return $request->user()?->timezone;
});
}

to be compatible with nova and the default customisation of timezone foreach user, the code should somehow look like this instead :

$d->setTimezone(new DateTimeZone($request->user()->timezone ?? config('app.timezone')))->translatedFormat($format)

if you see what i mean ?
Actually i'm facing the problem where the app default TZ is UTC, but users are coming from multiple tz and will only see the fields in the app default tz if i use this bundle.

Format being ignored...

Trying to set the format using globalFormat (and have tried inline as well). Ripping my hair out!

Getting an error as below..
Screenshot 2022-10-28 212715

Seems like globalFormat & have tried inline withDateFormat are being ignored.
Screenshot 2022-10-28 213054

Any ideas?

Nova 4.17.1

Everything up to date.

Laravel Nova v4.12.14 breaks this field

I just updated my nova installation to the latest version as described in the title and the datetime fields unfortunately no longer display anything.

As mentioned here they messed up the displayUsing function.

You probably don't have to do anything because it's their problem but I'll keep you posted if it's fixed or not in the next version.

Add a customizable Date/DateTime field with custom picker

Nova 4 defers formatting of Date/DateTime values in pickers to the browser by using its default picker implementation.

Many users have requested ways to customize the picker in different ways.

A custom DateTime field that implements its own customizable DateTime picker would be a super valuable addition to this package.

Datepicker timezone

image

I have a datetime field and works fine displaying and selecting the time in datepicker but when I try to save it tries to save the date with the tz, is there a way to remove that and save like '2023-01-05 00:00:00'?

image

Support timezone!

Hi! I configured timezone in config/app.php, for example:

    'timezone' => 'Europe/Moscow',

Then I added this DateTime field and Nova default DateTime field:

            \Wdelfuego\Nova\DateTime\Fields\DateTime::make(__('Date 1'), 'date'),
            \Laravel\Nova\Fields\DateTime::make(__('Date 2'), 'date'),

and I saw:

image

Nova default DateTime field show time with applying timezone.
This DateTime field show time without applying timezone.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.