您好,我有一个表,其中有一些列需要导出为货币,一些列需要导出为日期
但是数据表将日期导出为字符串。我知道为什么要按照用户指定的格式导出。但我的客户需要进行一些计算,他希望将列导出为日期而不是字符串。有人知道如何做到这一点吗?
注意*我已经尝试过正交选项=>它不起作用
下面是涉及该问题的代码:
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/