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>
如果日期是今天的日期,我需要显示当前图标,否则过去图标。如果条件总是失败
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');}
你需要稍微改变一下你的javascript代码。以下是修改
- getDay函数用于确定哪一天像星期一星期二等等
- 你需要用-1过一个月,因为它从-1开始
- 的日期比较,你应该使用
getTime()
,所以有更少