[英]How to compare and highlight date (with some condition) in DataTable?
"fnRowCallback": function( nRow, aData, iDisplayIndex, iDisplayIndexFull ) {
var date = new Date();
var date_now = ("0" + date.getDate()).slice(-2) + "-" + ("0" + (date.getMonth() + 1)).slice(-2) + "-" + date.getFullYear();
var date_compare = (new Date(aData[10]) < new Date(date_now) ? true : false);
console.log(date_now);
console.log(aData[10]);
console.log(date_compare);
if(date_compare == true) {
$('td', nRow).css('background-color', '#ff8080');
}
else {
$('td', nRow).css('background-color', 'none');
}
},
This isn't working. 这不起作用。
This coding is in the view inside javascript aData[10] is from model, the SQL look like this 该编码在javascript aData [10]里面的视图中来自模型,SQL看起来像这样
SELECT DATE_FORMAT(DB.TGL_JATUH_TEMPO, '%d-%m-%Y') AS TGL_JATUH_TEMPO
This is the result 这是结果
The result I want is true true false 我想要的结果是true true false
I want to compare like this if the date from my SQL Table < today's date then highlight red the column 如果我的SQL表中的日期<今天的日期,然后用红色突出显示该列,我想这样比较
The problem is the date compare didnt work 问题是日期比较没有用
Thank you for you help :) 谢谢你的帮助 :)
I think problem is with this line 我认为这条线有问题
(new Date(aData[10]) < new Date(date_now)
Its not converting to Date format. 它不会转换为日期格式。 If you can pass Date format as "YY-MM-DD" eg: 2010-12-25 this might work
如果您可以将日期格式传递为“ YY-MM-DD”,例如:2010-12-25,则可能可行
var dateVar = "2010-10-30";
var d=new Date(dateVar);
var currDate = new Date();
if(d < currDate){
alert("Entered Date is less than current Date");
}else{
alert("Entered Date is greated than current Date");
}
Your problem here is that the format of your date strings is not recognized by the Date
object , so it's always returning null
so they will be always equal , that's why your test always fail. 您的问题是
Date
对象无法识别日期字符串的格式,因此它始终返回null
因此它们始终相等 ,这就是测试始终失败的原因。
You need to build the Date object correctly, to respect the RFC2822 specifications so the Date
can be parsed by Date.parse()
. 您需要正确构建Date对象,以遵守RFC2822规范,以便
Date
可以通过Date.parse()
进行解析。
This is an example of how you could build your Date
objects: 这是如何构建
Date
对象的示例:
var d1 = "25-03-2018";
var date1 = new Date(d1.substr(d1.length - 4), d1.substr(3, 2) - 1, d1.substr(0, 2));
Demo: 演示:
This is a Demo with date comparison: 这是一个具有日期比较的演示:
var d1 = "25-03-2018"; var d2 = "26-03-2018"; console.log(d1.substr(d1.length - 4)); console.log(d1.substr(3, 2)); console.log(d1.substr(0, 2)); var date1 = new Date(d1.substr(d1.length - 4), d1.substr(3, 2) - 1, d1.substr(0, 2)); var date2 = new Date(d2.substr(d2.length - 4), d2.substr(3, 2) - 1, d2.substr(0, 2)); console.log(date1 < date2);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.