Datatable中的日期时间字段被排序为字符串。

huangapple go评论66阅读模式
英文:

Datatble datetime field is sorted as string

问题

我有一个用于我的Django Web应用程序的数据表格,后端发送的"updated"字段的日期格式为'Feb. 14, 2023, 3:58 p.m.'。当我使用排序功能时,它总是按字母顺序排序,其中'April 24, 2023, 10:04 a.m.'在'Feb'之前,因为在字母顺序中,A大于F,所以如何解决这个问题?我尝试了一些来自Datatable jQuery文档和一些StackOverflow答案,但它没有给我想要的输出。

英文:

I have a Datatble for my django web app, the backend sends date for the field updated as 'Feb. 14, 2023, 3:58 p.m.'. when I use the sorting feature, it always sorts as alphabetical order, where 'April 24, 2023, 10:04 a.m.' comes before Feb because A is bigger than F in alphabetical order, so how to solve this? I tries some from datatble jquery documentation and some StackOverflow answers but it didn't gave me the desired output

答案1

得分: 0

在导入 jQuery 后,导入 moment.js,并添加以下脚本:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>

然后添加以下脚本:

<script type="text/javascript">
    $("#datatble_id").DataTable({
        columnDefs: [
            {
                targets: [0], // 你的字段位置
                type: 'datetime',
                render: function (data, type, row, meta) {
                    var momentObj = moment(data, 'MMM. DD, YYYY, h:mm a');
                    if (type === 'sort') {
                        return momentObj.unix();
                    }
                    return momentObj.format('MMM. DD, YYYY, h:mm a');
                },
                orderable: true
            }
        ]
    });
</script>
英文:

after jquery importing, import momentjs

&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js&quot;&gt;&lt;/script&gt;

and add this script

&lt;script type=&quot;text/javascript&quot;&gt;
    $(&quot;#datatble_id&quot;).DataTable({
        columnDefs: [
            {
                targets: [0], // your field position
                type: &#39;datetime&#39;,
                render: function (data, type, row, meta) {
                    var momentObj = moment(data, &#39;MMM. DD, YYYY, h:mm a&#39;);
                    if (type === &#39;sort&#39;) {
                        return momentObj.unix();
                    }
                    return momentObj.format(&#39;MMM. DD, YYYY, h:mm a&#39;);
                },
                orderable: true
            }
        ]
    });
&lt;/script&gt;

答案2

得分: 0

以下是已翻译的内容:

使用这段PostgreSQL代码

使用虚拟表格

    创建表格test (
      datex文本
    )

插入数据

    插入到test (datex)(&#39;2023214日下午3:58&#39;);
    插入到test (datex)(&#39;2023424日上午10:04&#39;);


    选择*
    split_part(datex, &#39; &#39;, 1)--获取月份字符串
    rtrim(split_part(datex, &#39; &#39;, 2),&#39;,&#39;)--获取日期字符串
    rtrim(split_part(datex, &#39; &#39;, 3),&#39;,&#39;)--获取年份字符串
    EXTRACT(MONTH FROM TO_DATE(split_part(datex, &#39; &#39;, 1), &#39;Mon&#39;))--将月份转换为数字
    (rtrim(split_part(datex, &#39; &#39;, 3),&#39;,&#39;)||&#39;-&#39;||TO_CHAR(EXTRACT(MONTH FROM TO_DATE(split_part(datex, &#39; &#39;, 1), &#39;Mon&#39;)), &#39;fm00&#39;)||&#39;-&#39;||rtrim(split_part(datex, &#39; &#39;, 2),&#39;,&#39;))::日期 as newdat
    来自测试排序的

我使用"split part"函数拆分字符串,然后将其转换为日期列。

然后使用"order by"对日期列进行排序。

英文:

Use this postgres Code.

use dummy table as

create table test (
  datex text
)

Insert data

insert into test (datex) values (&#39;Feb. 14, 2023, 3:58 p.m.&#39;);
insert into test (datex) values (&#39;April 24, 2023, 10:04 a.m.&#39;);


select * ,
split_part(datex, &#39; &#39;, 1),--get Month string
rtrim(split_part(datex, &#39; &#39;, 2),&#39;,&#39;),--get day string
rtrim(split_part(datex, &#39; &#39;, 3),&#39;,&#39;),--get year string
EXTRACT(MONTH FROM TO_DATE(split_part(datex, &#39; &#39;, 1), &#39;Mon&#39;)),--convert month into number
(rtrim(split_part(datex, &#39; &#39;, 3),&#39;,&#39;)||&#39;-&#39;||TO_CHAR(EXTRACT(MONTH FROM TO_DATE(split_part(datex, &#39; &#39;, 1), &#39;Mon&#39;)), &#39;fm00&#39;)||&#39;-&#39;||rtrim(split_part(datex, &#39; &#39;, 2),&#39;,&#39;))::date as newdat
from test order by newdat

> I split the string using the "split part" function, then converted into the date column.

> After that use "order by" to sort the date column.

huangapple
  • 本文由 发表于 2023年2月24日 12:50:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75552731.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定