【问题标题】:Angular 7 Hide a div when created time is more than 6 monthsAngular 7在创建时间超过6个月时隐藏一个div
【发布时间】:2020-09-14 15:39:09
【问题描述】:

在 Angular 7 中,我想根据创建的时间隐藏一个 div,如果它是过去 6 个月,那么我想隐藏一个 div 如何在创建的时间超过 6 个月时修改 html 以隐藏 div

HTML。

<div class="request"  *ngIf="request.status != 'CANCELLED' && request.duration">

打字稿 /** 加载任务的持续时间 */

private getRequestDuration(request) {
    let createdDate = new Date(request.createTime);
    let timeInMilliSeconds = this.currentDate.getTime() - createdDate.getTime();
    let seconds = timeInMilliSeconds / 1000;
    let minutes = seconds / 60;
    let hours = minutes / 60 + 5; // FIXME: EST Offset
    hours = hours > 0 ? hours : 0; // FIXME: Hack to address time-stamp conversions / daylight savings time
    let days = hours / 24;
    return (days q> 0 ? Math.floor(days) + ' days, ' : '') + Math.floor(hours % 24) + ' hours';
}
requests.duration = this.getRequestDuration(requests);

Request Object
Object
agingCurrent: 121777671
agingTotal: 121902671
createTime: "2020-09-14T06:49:20"
createdBySso: "503184132"
duration: "1 days, 20 hours"
indErrored: false
indSavedToMdm: "TRUE"
indSubscribed: "TRUE"
partyId: "160598"
requestId: 627723
requestType: "Internal subscribe"
riskCategory: "ONE"
riskLevel: "MODERATE"
state: {status: "Active", label: "-"}
status: "APPROVED"
statusUpdateTime: "2020-09-14T06:51:25"
supplierId: "S18961"
supplierName: "LM Wind Power (Spain) SA"
transactionId: null
type: "SUBSCRIBE_INTERNAL"
updateTime: "2020-09-14T06:51:25"
version: 15

【问题讨论】:

  • 你为什么不用moment.js。它非常适合这种事情。这是一篇关于如何使用它的 SO 帖子。stackoverflow.com/questions/33440646/…
  • 嗨 Darren 属性 'getMonth' 在类型 'DateConstructor'.ts(2339) 上不存在
  • 如何解决这个错误

标签: javascript html angular typescript


【解决方案1】:

.ts 文件内部

public date = new Date();
public six_month_before_date = this.date.setMonth(this.date.getMonth() - 6);
//**convert request.createTime to Date object before compare**//
request.createTime=new Date(request.createTime)



 

.html 文件内部

<div class="request"  *ngIf="request.createTime>six_month_before_date">
<!-Only then display data-->
</div>

【讨论】:

  • 嗨桑杰让我试试
  • 我应该在 .ts 文件的什么地方写下你给的函数
  • 我应该创建任何方法,例如 private Six_month_before_date=new Date() Six_month_before_date.setMonth(date.getMonth() -6);// 减去六个月
  • Hi Sanjay Property 'getMonth' does not exist on type 'DateConstructor'.ts(2339) 如何解决此错误
  • public date=new Date() public Six_month_before_date=date.setMonth(date.getMonth() -6);
猜你喜欢
  • 1970-01-01
  • 2014-10-28
  • 1970-01-01
  • 2012-02-25
  • 1970-01-01
  • 2013-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多