Table中的数据,把时间去掉秒,显示没有去除成功,如何解决
时间: 2024-03-05 21:49:40 浏览: 8
如果在Table中显示的时间无法成功去除秒钟部分,可能是因为Table组件自带的渲染函数默认会将时间转换为字符串类型,而转换为字符串时会保留秒钟部分。可以通过自定义渲染函数来解决这个问题。
假设时间字段的名称为 `time`,可以在Table组件中使用 `render` 属性来指定自定义渲染函数,示例代码如下:
```
import moment from 'moment';
import { Table } from 'antd';
const dataSource = [
{
id: 1,
time: '2022-06-27 11:00:00'
},
{
id: 2,
time: '2022-06-28 12:00:00'
}
];
const columns = [
{
title: 'ID',
dataIndex: 'id'
},
{
title: '时间',
dataIndex: 'time',
render: (text) => {
// 使用moment.js进行时间格式化,去除秒钟部分
return moment(text).format('YYYY-MM-DD HH:mm');
}
}
]
function MyTable() {
return (
<Table dataSource={dataSource} columns={columns} />
);
}
```
在自定义渲染函数中,使用moment.js的format()方法进行格式化,去除秒钟部分,然后返回格式化后的时间字符串。这样Table中显示的时间就可以去除秒钟部分了。