wordpress - Ionic 2 和 wordpress(如何“一分钟前、几小时前等”)

标签 wordpress angular ionic-framework ionic2

我在分配 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/

相关文章:

php - 清理 WordPress 帖子循环中的 the_title()

Angular react 形式,添加和删除字段?

css - ionic : how to hide ion slide pager on first slide

css - 无法更改 bootstrap-datepicker 的颜色

css - 具有不同轮廓重量的图标 - 是否可以调整?

ionic-framework - Ionic3 - 如何使用 ion-select 和 ion-option 将选定的值传递给事件?

php - 如何使用 <use> 加载外部 svg 文件?在 WordPress 中?

mysql - 从数据库导入表

javascript - 如何将 WordPress 帖子 ID 传递到 HTML5 本地存储

javascript - 根据环境有条件地在 Angular 中加载一组不同的工厂