您好,我有一个表,其中有些列需要导出为货币,有些列需要导出为日期
但是数据表将日期导出为字符串。我知道以用户指定的格式导出的原因。但是我的客户需要做一些计算,他希望将列导出为日期而不是字符串。有没有人知道如何去做?
注意*我试过正交选项=>它不工作
下面是引用问题的代码:
var buttonCommon = {
exportOptions: {
format: {
body: function ( data, column, row ) {
// Strip $ from salary column to make it numeric
return column === 5 ?
data.replace( /[$,]/g, '' ) :
data;
}
}
}
};
此处用户正在将字符串列转换为数字。有什么东西可以将字符串转换为日期吗?
我试过这段代码
exportOptions: {
format: {
body: function ( data, column, row ) {
// Strip $ from salary column to make it numeric
var customDate = new Date('2016/10/10');
customDate = (customDate.getMonth() + 1) + '/' + customDate.getDate() + '/' + customDate.getFullYear();
return column === 12 ? customDate: data;
}
},
columns: ':visible',
},
但它仍然返回 2016 年 10 月 10 日 2016 年 10 月 10 日 2016 年 10 月 10 日 2016 年 10 月 10 日
*作为字符串
最佳答案
希望它对你有用。 根据你的代码添加下面的代码,它检测日期格式为dd/mm/yyyy的数据
jQuery.fn.dataTableExt.aTypes.unshift(
function ( sData )
{
if (sData !== null && sData.match(/^(0[1-9]|[12][0-9]|3[01])\/(0[1-9]|1[012])\/(19|20|21)\d\d$/))
{
return 'date-uk';
}
return null;
}
);
要使用此代码,您必须使用此代码
<script src="//cdn.datatables.net/plug-ins/1.10.12/type-detection/date-uk.js"/>
关于Jquery 数据表将列导出为类型 "Date",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38244872/