react table column日期转换

首先定义一个日期转换组件,我这就随便找了一个,大体如下:

这里我引用一位大佬造好的轮子,出处:https://blog.csdn.net/weixin_39466493/article/details/79565773

class DateAPI {

/**

* 将输入的毫秒字符串or毫秒数转换成指定的字符串格式

* @param {string} msStr 毫秒字符串 or 毫秒数

* @param {string} format yyyy-MM-dd or yyyy-MM-dd hh:mm:ss

* @return {string} 转换后的字符串

*/

static format(msStr, format) {

const date = new Date(msStr / 1);

let fmt = format;

const obj = {

'M+': date.getMonth() + 1, // 月份

'd+': date.getDate(), // 日

'h+': date.getHours(), // 小时

'm+': date.getMinutes(), // 分

's+': date.getSeconds(), // 秒

'q+': Math.floor((date.getMonth() + 3) / 3), // 季度

S: date.getMilliseconds(), // 毫秒

};

if (/(y+)/.test(fmt)) {

fmt = fmt.replace(RegExp.$1, (String(date.getFullYear())).substr(4 - RegExp.$1.length));

}

const keys = Object.keys(obj);

for (let i = 0; i <= keys.length; i += 1) {

const k = keys[i];

if (new RegExp(`(${k})`).test(fmt)) {

fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (obj[k]) : ((`00${obj[k]}`).substr((String(obj[k])).length)));

}

}

return fmt;

}

/**

* 获得传入的秒字符串msStr距离现在时间的字符串,格式为 昨天 今天 明天 当前年3月4日 其余2000-01-01

* @param {string} msStr 毫秒字符串

* @return {string} 可以显示用的字符串

*/

static format2(msStr, showYear) {

let ms = new Date(DateAPI.format(msStr, 'yyyy/MM/dd')).getTime() / 1000;

const now = new Date(DateAPI.format(new Date().getTime(), 'yyyy/MM/dd')).getTime() / 1000;

ms = now - ms;

const min = 60;

const hour = 60 * min;

const day = 24 * hour;

if (ms === 0) {

return '今天';

}

if (ms === day) {

return '昨天';

}

if (ms === -day) {

return '明天';

}

const date = new Date((msStr / 1));

const nowYear = new Date().getFullYear();

const year = date.getFullYear();

const month = date.getMonth() + 1;

if (showYear === false && year === nowYear) {

return `${month}月${date.getDate()}日`;

}

return `${date.getFullYear()}年${month}月${date.getDate()}日`;

}

/**

*获取当前天数之前或者之后的日期

*@num 之前或者之后多少天数 - 表示之前

*/

static getDate(num, format) {

const today = new Date();

const targetday = today.getTime() + (1000 * 60 * 60 * 24 * num);

const target = new Date();

target.setTime(targetday);

if (format) {

return this.format(target.getTime(), format);

}

return target;

}

}

export default DateAPI;

接下来重点来了,如何使用,如下:

1.在我们的模块里面引入 组件

import DateAPI from './DateAPI'

class User extends Component {

2.在我们这个组件里面需要转换的列的位置,使用我们的组件:

{ title: '部门', width:160 ,dataIndex: 'address', key: '4' },

{ title: 'create-time',width:160, dataIndex: 'createTime', key: '5' , render:(value,Object)=>{

return DateAPI.format(value,'yyyy-MM-dd hh:mm:ss');

} },

我解释一下,这里的 value 即当前行当前列的值,Object 即为当前行对象,知道这点之后,我想大家就可以举一反三,进行其他想干的坏事了吧 !  手动  滑稽 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
React 中自定义 Table Column 标题可以通过两种方式实现: 1. 使用自定义组件作为 Column 标题 可以通过使用自定义组件来渲染 Table Column 标题。在 Column 的 `title` 属性中传入一个函数,该函数返回一个自定义组件,例如: ```jsx import React from "react"; import { Table } from "antd"; const columns = [ { title: () => <div>自定义标题1</div>, dataIndex: "name", key: "name" }, { title: () => <div>自定义标题2</div>, dataIndex: "age", key: "age" } ]; const data = [ { key: "1", name: "John Brown", age: 32 }, { key: "2", name: "Jim Green", age: 42 } ]; const CustomTable = () => { return <Table columns={columns} dataSource={data} />; }; export default CustomTable; ``` 2. 使用对象方式定义 Column 标题 在 Column 中可以直接使用对象的方式定义标题,并设置样式,例如: ```jsx import React from "react"; import { Table } from "antd"; const columns = [ { title: ( <div style={{ color: "#f00", fontWeight: "bold" }}>自定义标题1</div> ), dataIndex: "name", key: "name" }, { title: ( <div style={{ color: "#00f", fontWeight: "bold" }}>自定义标题2</div> ), dataIndex: "age", key: "age" } ]; const data = [ { key: "1", name: "John Brown", age: 32 }, { key: "2", name: "Jim Green", age: 42 } ]; const CustomTable = () => { return <Table columns={columns} dataSource={data} />; }; export default CustomTable; ``` 以上两种方式都可以实现自定义 Table Column 标题的效果,具体选择哪种方式可以根据实际需要来决定。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机智の小盆友

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值