2017-02-07 2 views
2

私は現在の時間を取得するためのDate.now()//私の仮定を割り当てるいくつかの問題があります 私は私のワードプレス。その後、イオンでは、私は「など、日前に、時間前に」のようなポストの時間を設定したい ここに私のコードだイオニック2&ワードプレス(どのように1分前、何時間前など)

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

@Pipe({ 
    name: 'timesago' 
}) 
@Injectable() 
export class Timesago { 

    transform(value, args) { 

    let now = Date.now(); 
    let timepost = (now - value)/1000; 

    if (timepost < 60) { 
     return `${Math.floor(timepost)}second ago`; 
    } else if (timepost < 3600) { 
     return `${Math.floor(timepost/60)}minute ago`; 
    } else if (timepost < 86400) { 
     return `${Math.floor(timepost/3600)}hour ago`; 
    } else { 
     return `${Math.floor(timepost/86400)}day ago`; 
    } 
    } 
} 

出力さ:NAND型日前

「値」日付wordpressの投稿です私はconsole.log(値)を使用しているときにそれを知っています console.log(現在)を使用しようとしました; //出力は "1745432145"になります。現在時刻のデータかどうかはわかりませんが、私はそれがだと思います調整しないでください。

答えて

2

問題は、プラグインmoment.jsで解決します。そのプラグインはとても愛らしい。私の友人は、そのプラグインを使用するように教えてくれます。

は、ここでは、ポスト

import { Injectable, Pipe } from '@angular/core'; 
import * as moment from 'moment'; 

/* 
    Generated class for the Timesago pipe. 

    See https://angular.io/docs/ts/latest/guide/pipes.html for more info on 
    Angular 2 Pipes. 
*/ 
@Pipe({ 
    name: 'timesago' 
}) 
@Injectable() 
export class Timesago { 
    now:any; 

    /* 
    Takes a value and makes it lowercase. 
    */ 
    transform(value, args) { 
    this.now = moment(value).fromNow(); 
    return this.now; 
    } 
} 

に「など、日前」プラグインのパワーが非常に簡単にする私のコードです。

関連する問題