简体   繁体   English

仅当日期变量在当前月份中时才在表中显示行

[英]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.

相关问题 在日期选择器中进行验证,仅显示当前日期起的最近5个月的日期 - validation in date picker, only show last 5 month date from current date 如何在日期选择器中仅显示 3 个月作为当前日期的下拉列表,当当月完成时,我们将显示即将到来的 3 个月 - How to show only 3 months in datepicker as dropdown from current date and when the current month is completed then we have show the upcoming 3 months React-big-calendar仅显示当前月份和下个月 - React-big-calendar Show only current month and next month 新的Date()仅显示年份或月份和年份 - new Date() only show the year or month and year 自定义日历以显示当前日期的下六个月日历 - Custom Calendar to show next six month calendar to the current Date 当前,上个月和下个月日历显示在日期选择器中 - Current, Previous and Next month calendar show in date picker 仅显示当前行的保存按钮 - Have the save button show only for the current row 在fullcalendar.io中仅显示当月的日期和事件 - Show days and events only of the current month in the fullcalendar.io 显示带有当前表格行悬停信息的 div - Show div with information for the current table row hover 日期验证-仅输入当月内的日期 - Date Validation - Only enter dates within current month
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM