dat/datetime field, how can I strip the time off?

dat/datetime field, how can I strip the time off?

washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

I am using ASPNET Core and my DateTime's are being serialized back like so: "2018-12-25T15:19:25.5594555"

I tried to use the field type of "date" but HTML5 does not know how to parse that and there is not an option to specify the input format yet.

I tried to use "datetime" (without jQuery UI) but the format does not seem to control the output of the initial set until a date is picked via the picker.

One solution is to use the AddJsonOptions IMvcCoreBuilder member to set MvcJsonOptions via something like this:
options.SerializerSettings.DateFormatString = "yyyy-MM-dd"; but I do not want to go messing with Core defaults and cause issues for other clients.

Ideally, DataTables would initialize the datetime field type with the format I specified. Also, I am aware this exists: https://datatables.net/plug-ins/sorting/datetime-moment but hopefully I am missing something simple and I dont have to pull this in and go updating all of my field definitions.

Answers

  • washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

    Currently have something like this.

                {
                    "name": "DockDate",
                    "data": function (data, type, set) {                    
                        // 'editor' is setting the value.
                        if (type === 'editor') {
                            data.DockDate = moment(data.DockDate).format("MM-DD-YYYY");
                        }
                        return data.DockDate;
                    },
                    "label": "Dock Date",
                    "type": "datetime",
                    "format": 'MM-DD-YYYY',
                    "className": "block",
                    "attr": {
                        "autocomplete": "off"
                    }
                },
    

    This will zero out the time on submit if there existed one before, which I think is fine. It would be nice to have some display only representation. Until edited.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Hi,

    Assuming you are using the .NET Core libraries for Editor, use a get / set formatter - e.g.:

                        .Field(new Field("registered_date")
                            .Validator(Validation.DateFormat(
                                registered,
                                new ValidationOpts { Message = "Please enter a date in the format dddd d MMMM yyyy" }
                                ))
                            .GetFormatter(Format.DateSqlToFormat("dddd d MMMM yyyy"))
                            .SetFormatter(Format.DateFormatToSql("dddd d MMMM yyyy"))
    

    Allan

  • washuit-iammwashuit-iamm Posts: 133Questions: 55Answers: 2

    Sorry Alan, I am using this library and not the provided Core Libraries because I want to use Entity Framework Core. https://github.com/AlexanderKrutov/DataTables.Queryable

This discussion has been closed.