Dateformat to dd.mm.yyyy editortables

Dateformat to dd.mm.yyyy editortables

tableFantableFan Posts: 10Questions: 7Answers: 0

Hello,
I've been trying to set the date to dd.mm.yyyy for days but it doesn't work. I tried with moment, but unfortunately it didn't work, can someone help me? This is my code:

 public ActionResult EditorTable()
        {

            //var connString = Configuration.GetConnectionString("DevContext");
            string dbConnection = this._configuration.GetSection("ConnectionStrings")[connection];
            //DbProviderFactories.RegisterFactory("Microsoft.Data.SqlClient", Microsoft.Data.SqlClient.SqlClientFactory.Instance);

            using (var db = new Database("azure", dbConnection))

            {
                var response = new Editor(db, "Model")
                    .Model<Model>()

                    .Field(new Field("column1"))
                    .Field(new Field("column2"))
                   .........
                    .Field(new Field("columnDATE"))
                    .TryCatch(false)
                    .Process(Request)
                    .Data();
                return Json(response);
            }

        }


var editor;  
        $(document).ready(function(){
            editor = new $.fn.dataTable.Editor ({
                ajax: {url: "/Controller/EditorTable" , type: "POST"},
                table: "#tbl",
                fields: [{
                    label: "column1",
                    name: "column1",
                    type: "select",
                    options: dropdownColumn1.value
                    },
                   ..........................
                    {
                    label: "datum",
                    name: "columnDATE",
                    type: "datetime",
                    def:   function () { return new Date(); }
                    }
                ]
            });

Answers

  • rf1234rf1234 Posts: 2,997Questions: 87Answers: 421
    edited July 2022

    Using moment.js this line should give you today's date in the format you require. And yes: This is case sensitive! dd.mm.yyyy will NOT work.

    ....
    def: function () { return moment().format('DD.MM.YYYY') }
    

    If you want to achieve the same using plain javascript it gets a little complicated. I wouldn't use it. You need to get moment.js going anyway, e.g. for international date sorting and the like using the universal date sorting plug in (or whatever it was called).
    https://datatables.net/plug-ins/sorting/datetime-moment

    let today = new Date();
    let pad = '00';
    let hlp = today.getMonth() + 1;
    hlp = hlp.toString();
    let month = pad.substr(0, pad.length - hlp.length) + hlp;
    hlp = today.getDate().toString();
    let day = pad.substr(0, pad.length - hlp.length) + hlp;
    let year = today.getFullYear().toString();
    let DDdotMMdotYYYY = day + '.' + month + '.' + year;
    
    
Sign In or Register to comment.