简体   繁体   English

如何 JS 日期格式

[英]How to JS Date Format

I'm using Laravel 8 and Bootsrap in this project.我在这个项目中使用 Laravel 8 和 Bootsrap。 I've tried to solve this problem with Carbon in Laravel, but the prob still likes that.我试图用 Laravel 中的 Carbon 来解决这个问题,但问题仍然存在。

So I think maybe the problem in JS Script, but I dunno about JS so deep.所以我认为可能是 JS 脚本中的问题,但我不知道 JS 这么深。 I really need help with this prob.我真的需要这个问题的帮助。

My problem is how to change the date format in JS, I wish to change in DD/MM/YYYY format.我的问题是如何更改 JS 中的日期格式,我希望更改为 DD/MM/YYYY 格式。

JS Script JS 脚本

 <script> $(function() { $('#kategoris-table').DataTable({ processing: true, serverSide: true, ajax: 'kategori/json', columns: [ { data: 'id', name: 'id' }, { data: 'nama', name: 'nama', fnCreatedCell: function (nTd, sData, oData, iRow, iCol) { $(nTd).html("<a href='/kategoris/"+oData.id+"'>"+oData.nama+"</a>"); } }, { data: 'updated_at', name: 'updated_at'}, ] }); }); </script>

My Table View我的表视图在此处输入图像描述

Add this variable to the Model class:将此变量添加到 Model class 中:

protected $casts = [
    'updated_at' => 'datetime:m/d/Y'
];

This will change the datetime format and will reflect whenever you retrieve the data through eloquent.这将更改日期时间格式,并在您通过 eloquent 检索数据时反映出来。

You could try registering an editor and using moment.js .您可以尝试注册一个editor并使用moment.js Something like this:像这样的东西:

var editor; // use a global for the submit and return data rendering in the examples

$(document).ready(function() {
    
    $.fn.dataTable.moment( 'DD/MM/YYYY' );

    editor = new $.fn.dataTable.Editor( {
        ajax: 'kategori/json',
        table: '#kategoris-table',
        fields: [ {
                label:  'Updated at:',
                name:   'updated_at',
                type:   'datetime',
                def:    function () { return new Date(); },
                format: 'DD/MM/YYYY',
                fieldInfo: 'Formatted date'
            }
        ]
    } );

    $('#kategoris-table').DataTable({
        processing: true,
        serverSide: true,
        ajax: 'kategori/json',
        columns: [
            { data: 'id', name: 'id' },
            { data: 'nama', name: 'nama',
                fnCreatedCell: function (nTd, sData, oData, iRow, iCol) {
                    $(nTd).html("<a href='/kategoris/"+oData.id+"'>"+oData.nama+"</a>");
                }
            },
            { data: 'updated_at', name: 'updated_at'},
        ]
    });
});

Please see this example which includes the other libraries you would need to load at the end of the example: https://editor.datatables.net/examples/dates/formatting.html请参阅此示例,其中包括您需要在示例末尾加载的其他库: https://editor.datatables.net/examples/dates/formatting.html

PS - I tried to include the libraries in my answer in case of a broken link but when I do, I am given the message that "There appears to be code in my post that is not formatted properly" and I cannot seem to get it to work with formatting the links. PS - 我试图在我的答案中包含这些库以防链接断开,但是当我这样做时,我收到消息“我的帖子中似乎有代码格式不正确”,我似乎无法得到它使用格式化链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM