-1
    connectedCallback()
{
    var q = new Date();
    var m = q.getMonth()+1;  //january is 0!
    var d = q.getDay();
    var y = q.getFullYear();        
    //var date = new Date(y,m,d);        
    var formattedDate = new Date(y,m,d);     
    var d=new Date('2021-10-05');        
    if( formattedDate === d)
    {this.currentIcon=true;}
    else {this.pastIcon=true;}

<template>
<template if:true={pastIcon}>
    <lightning-dynamic-icon type="trend" option="down" alternative-text="Trending down">
    </lightning-dynamic-icon>
    <template if:true={currentIcon}>
        <lightning-dynamic-icon type="trend" option="up" alternative-text="Trending up">
        </lightning-dynamic-icon>
    </template>
</template>

if date is todays date i need to display currenticon, else past icon. if condition is always failing.

1 Answers1

0
var q = new Date();
var m = q.getMonth();
var d = q.getDate();
var y = q.getFullYear();      
var formattedDate = new Date(y,m,d).getTime();     
var d = new Date('2021','09','06').getTime();  //Months start from zero so you need to pass -1
if( formattedDate === d)
{console.log('equal');}
else {console.log('past');}

you slightly need to change your javascript code. Below are the changes

  1. getDay function is to determine which day it is like Monday Tuesday etc etc
  2. You need to pass a month with -1 because it starts from -1
  3. for date comparison, you should use getTime() so there is less chance of error.
Taylor Rahul
  • 709
  • 6
  • 19