2017-12-07 12 views
-1

今日の日付とデータの入力日付をデータベースに比較することで、日時の数を表示したいと考えています(例2日前、今すぐ)。 私は、エントリの日付フィールドを使用し、msToTime(s)を使用して秒に変換します。 list.ENTDTを秒(this.diff)で置き換えるにはどうすればいいですか?日数や時間を表示する方法は?

<ion-list no-lines *ngFor="let list of displayList;let i=index;" > 

    <ion-item text-wrap *ngIf="list.STATUS=='ACTIVE'" style="padding-left:30px;padding-right:30px;"> 
    <p style="color: #818993;margin-top:-20px;font-size: 15px;" (click)="ind(list)">{{list.ENTUSR }} 
     <b style="font-size: 40px;margin-top: -50px;" [style.color]="list.PRIORITY == 'HIGH'? 'red': list.PRIORITY == 'MEDIUM'? '#fece60':'#699271'">.</b></p> 

     <p style="font-size: 10px;margin-top:-5px;" [style.color]="list.ENTDT > list.DEADLINE_DT ? 'red' : '#9DA4AB'"> {{list.ENTDT}}</p> 

     <p *ngIf="list.flg[0].SHOW_ICON=='YES'" style="text-align:center;margin-top:-35px;background-color:#3B4148;color: #ffffff;margin-left:98%;border-radius:50%;">1 </p> 

     <hr style="margin-top:25px;"> 
     <div style="height:35px;" onclick="style='height:auto'"> 

     <p style="color: #9DA4AB;font-size: 15px;" >{{list.TASKDESC}}</p> 
     </div> 
    </ion-item> 
    </ion-list> 

.TS

ngOnInit() { 
     this._myservice.myservice(this.taskdesc).subscribe(data => { 
      this.dataa = data; 
      this.displayList= data; 
      this.ENTUSR = data.map(e => e.ENTUSR); 
      this.TASKDESC = data.map(e => e.TASKDESC); 
      console.log(this.ENTUSR); 
      this.ENTDT = data.map(e => e.ENTDT); 
      console.log(this.ENTUSR); 
      console.log(this.dataa); 
      console.log(this.displayList, "display"); 

      console.log(this.displayList.length); 
      for(let i=0; i<=this.displayList.length; i++) { 



      this.past = new Date(this.ENTDT[i]); 
      console.log(this.past); 
      this.now = new Date(); 
      this.diff = msToTime(this.now - this.past); 
      console.log("difference"); 
      console.log(this.diff, "difference"); 
      } 

     function msToTime(s) { 
     let ms = s % 1000; 
     s = (s - ms)/1000; 
     let secs = s % 60; 
     s = (s - secs)/60; 
     let mins = s % 60; 
     let hrs = (s - mins)/60; 
     if (hrs == 0 && mins == 0) 
      return 'just a moment ago'; 
     else if (hrs == 0) 
      return mins + ' mins ago'; 
     else if (hrs < 24) 
      return hrs + ' hours ago'; 
     else 
      return Math.floor(hrs/24) + ' days ago'; 
     } 

アドバイスしてください。

+0

https://www.npmjs.com/package/time-ago-pipe – 99tharun

+0

ありがとう.. –

答えて

0

あなたはapp.Module.ts

使用中にそれを追加コメントで指摘したように外部モジュールを使用するか、同じ

import{PipeTransform,Pipe} from '@angular/core'; 

@Pipe({ 
    name:'relativeTime' 
}) 

export class RelativeTimeFilterPipe implements PipeTransform{ 

    transform(inputDate:string):string{ 
    var current = new Date().valueOf(); 
    var input = new Date(parseInt(inputDate)).valueOf(); 
    var msPerMinute = 60 * 1000; 
    var msPerHour = msPerMinute * 60; 
    var msPerDay = msPerHour * 24; 
    var msPerMonth = msPerDay * 30; 
    var msPerYear = msPerDay * 365; 

    var elapsed = current - input; 

    if (elapsed < msPerMinute) { 
     return Math.round(elapsed/1000) + ' seconds ago'; 
    } 

    else if (elapsed < msPerHour) { 
     return Math.round(elapsed/msPerMinute) + ' minutes ago'; 
    } 

    else if (elapsed < msPerDay) { 
     return Math.round(elapsed/msPerHour) + ' hours ago'; 
    } 

    else if (elapsed < msPerMonth) { 
     return 'approximately ' + Math.round(elapsed/msPerDay) + ' days ago'; 
    } 

    else if (elapsed < msPerYear) { 
     return 'approximately ' + Math.round(elapsed/msPerMonth) + ' months ago'; 
    } 

    else { 
     console.log('inside the if condition', elapsed); 
     return 'approximately ' + Math.round(elapsed/msPerYear) + ' years ago'; 
    } 

    } 
} 

のためのパイプの作成を選択しますか、テンプレート

<span class="label label-success edit-btn"> - {{i.createdAt | relativeTime}} by {{i.name}}</span> 

Live example

関連する問題