Home > OS >  Compare current date with date in data lists (don't compare for the second of date)
Compare current date with date in data lists (don't compare for the second of date)

Time:10-05

I want to compare 2 date (don't compare for the second of date): current date and date in data list. If currentDate === the date in lists -> field isCurrentDate will change.

Example:

const currentdate = new Date();

works: any[]=[

{
id: 1,
dateWork:'2022-10-08T10:30:04.000Z',
isCurrentDate: false,
},

{
id: 1,
dateWork:'2022-10-07T11:30:04.000Z',
isCurrentDate: false,
},

{
id: 1,
dateWork:'2022-10-06T13:30:04.000Z',
isCurrentDate: false,
}
]

CodePudding user response:

try this:

var currentdate = new Date();
currentdate.setSeconds(0,0)

var works=[

  { id: 1, dateWork:'2022-10-08T10:30:04.000Z', isCurrentDate: false, },
  
  { id: 1, dateWork:'2022-10-07T11:30:04.000Z', isCurrentDate: false, },
  
  { id: 1, dateWork:'2022-10-06T13:30:04.000Z', isCurrentDate: false, } ]
  
  works.forEach(function (value, index) {

    var dateWork = (new Date(value.dateWork))
    dateWork.setSeconds(0,0)
    if(currentdate.getTime()===dateWork.getTime())
    {
      works[index].isCurrentDate=true;
    }
  });
  • Related