[英]Show row in table only if date variable is in current month
I draw table in my script where I display some data. 我在显示一些数据的脚本中绘制表格。 One of columns in table is date variable.
表中的列之一是日期变量。
If that date variable is in current month, I want its row to be displayed in table. 如果该日期变量在当前月份,则希望其行显示在表中。 Other words, if date is not in current month I want to hide whole row.
换句话说,如果日期不是当前月份,我想隐藏整行。
Date column is originally in ISO date format, but I convert it into DD.MM.YYYY. 日期列最初是ISO日期格式,但我将其转换为DD.MM.YYYY。 with this function:
具有此功能:
var Date1 = MyDate;
function formatYMDtoDMY(s)
{
var b = s.split(/\D/);
return b[2] + '.' + b[1] + '.' + b[0] + '.';
}
than I call this function in my table, 3rd column in table: 比我在表中第3列的表中调用此函数的时间要多:
$("#T1").append("<tr align='middle'>" +
"<td align='left'>"+ID+"</td>" +
"<td align='left'>"+UserName+"</td>" +
"<td align='left'>"+formatYMDtoDMY(Date1)+"</td>"+
"<td align='left'>"+City, State+"</td>" +
"<td align='left'>"+Project name+"</td>" +
"<td align='left'>"+Additional info+"</td>" +
"</tr>");
I tried this way: 我这样尝试:
if(input.getFullYear() == currentDate.getFullYear()
&& input.getMonth() == currentDate.getMonth()) {
// than I post my table
} // but all I get displayed is only 1st date
I also thought I could catch 1st and last day of current month like this: 我还认为我可以像这样捕获当前月份的第一天和最后一天:
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);
but I don't know how to proceed with checking date column if it is between these variables and if yes to display row in my table. 但是我不知道如何检查日期列是否在这些变量之间以及如果要在表中显示行。
So I completely misunderstood your question. 所以我完全误解了你的问题。
The easiest thing will be to convert your ISO date into a real date. 最简单的方法是将ISO日期转换为实际日期。 The JavaScript Date object understands the ISO date format, so you can create a date object by calling the constructor:
JavaScript Date对象可以理解ISO日期格式,因此您可以通过调用构造函数来创建date对象:
var myDateObj = new Date(MyDate);
Date objects work with logical operators, so we can use your firstDay and lastDay objects: 日期对象与逻辑运算符一起使用,因此我们可以使用您的firstDay和lastDay对象:
var isCurrent = (firstDay <= myDateObj && myDateObj <= lastDay);
You can use this to show and hide rows as you render your table. 您可以在渲染表格时使用它来显示和隐藏行。
$("#T1").append("<tr align='middle' class='" + isCurrent ? 'row-visible' : 'row-hidden' +"'>" +
"<td align='left'>"+ID+"</td>" +
"<td align='left'>"+UserName+"</td>" +
"<td align='left'>"+formatYMDtoDMY(Date1)+"</td>"+
"<td align='left'>"+City, State+"</td>" +
"<td align='left'>"+Project name+"</td>" +
"<td align='left'>"+Additional info+"</td>" +
"</tr>");
Add CSS rules: 添加CSS规则:
.row-hidden {
display: none;
}
.row-visible {
display: table-row;
}
You could wrap this up into a single function: 您可以将其包装为一个函数:
function isCurrentMonth(s) {
var myDate = new Date(s);
var date = new Date(), y = date.getFullYear(), m = date.getMonth();
var firstDay = new Date(y, m, 1);
var lastDay = new Date(y, m + 1, 0);
return (firstDay <= myDate && myDate <= lastDay);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.