我有一个离子卡,可以显示从firebase读取的信息。1这样的信息就是日期。如果日期等于今天,那么我想做一些样式。
在我的html ion-card-content标记中,我有以下代码
<ion-card-content [ngClass]="isMatchDateToday(match?.date.seconds * 1000)">
我想调用一个函数,该函数将日期作为参数传入,并比较今天的日期。
isMatchDateToday(date: Date){
}
我真的不知道如何比较日期。我使用了console.log = date.now(),并尝试将它与我的date进行比较,但是,当date的值具有不同长度的长数值时,它们的长度是不同的。
我预计会出现这样的情况(pesudocode)
isMatchDateToday(date: Date){
var today = Date.now();
if (date == today)
{
return "css-class-name";
}
}
发布于 2019-09-12 16:36:22
示例-(丢弃小时、分钟、秒)
// sample method to generate the Date object
const firetimestamp = new Date(firebasetimestamp.seconds * 1000);
// sample dates
const entry1 = new Date('2019-09-12');
const entry2 = new Date('2019-09-13');
您可以选择使用Date.prototype.toISOString()。https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString
export function Compare(value: Date): string {
const today: Date = new Date();
if (today.toISOString().substr(0, 10) === value.toISOString().substr(0, 10)) {
return "success class-name";
} else {
return "error class-name";
}
}
console.log(Compare(entry1));
console.log(Compare(entry2));
注意:从客户端而不是从服务器获取今天的日期
https://stackoverflow.com/questions/57909689
复制相似问题