我在分配 Date.now() 时遇到一些问题//我对获取当前时间的假设。 我已经在我的 WordPress 中发表了一篇文章。然后在 ionic 中我想将发布时间设置为“一小时前、几天前等” 这是我的代码
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 天前
“value”是日期wordpress帖子,当我使用console.log(value)时我知道它。 我尝试过使用 console.log(now);//输出为“1745432145”,我不知道它是否是当前时间的数据。但是,我认为这不是调整。
最佳答案
使用插件 moment.js 解决问题。那个插件太可爱了。我的 friend 告诉我使用该插件。
这是我在帖子中制作“几天前等”的代码
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;
}
}
很简单,插件的力量。
关于wordpress - Ionic 2 和 wordpress(如何“一分钟前、几小时前等”),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42101668/