Angular 2 + Ionic 2:如何更改控制器中的日期格式?
Angular 2 + Ionic 2: how to change the date format in controller?
我正在从 sqlite 数据库循环获取日期。格式类似于 "Wed Feb 01 2017 21:54:24 GMT-0200"。但我想将其更改为 'dd/MM/yyyy'。
如何在控制器中更改格式?
例如我的代码:
this.issuedService.fetchAll().then(( res ) => {
var data = [];
for (let entry of <Array<any>>res) {
console.log( JSON.stringify( entry ) );
}
//this.items = <Array<any>>res;
}, ( error ) => {
console.log( "ERROR: ", error.message );
});
更新 2017-02-02
于是,几个小时没睡觉,我终于找到了解决办法。
我可以从 NPM 将 MomentJs 库添加到我的 Ionic v2 项目中。
$ npm install moment --save
结果代码:
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import * as moment from 'moment';
import 'moment/locale/pt-br';
import { IssuedService } from '../../providers/issued-service';
@Component( {
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
items: any[] = [];
issuedPage = IssuedPage;
constructor(
public navCtrl: NavController,
public navParams: NavParams,
public issuedService: IssuedService
) {
this.initializeItems();
}
ionViewDidLoad() {
console.log( 'ionViewDidLoad HomePage' );
}
initializeItems() {
this.issuedService.fetchAll().then(( res ) => {
for ( let entry of <Array<any>>res ) {
entry.query_dt = moment( entry.picking_dt ).format( 'DD MMMM YYYY' );
this.items.push(entry);
}
console.log( JSON.stringify( this.items ) );
}, ( error ) => {
console.log( "ERROR: ", error.message );
});
}
}
试试这个
$scope.date = 'Wed Feb 01 2017 21:54:24 GMT-0200';
var filterdatetime = $filter('date')( $scope.date,'dd/MM/yyyy' );
alert(filterdatetime);
我项目的解决方案:
Adding third party libs
You can add most third party libraries to your V2 project from NPM.
For example let’s add MomentJs.
$ npm install moment --save
From here, we can import it into what ever class we want to use it in.
import {Page} from 'ionic-angular';
import * as moment from 'moment';
export class MyClass {
constructor(){
moment("20111031", "YYYYMMDD").fromNow();
}
}
我用两行做到了:
import { DatePipe } from '@angular/common';
<ion-label>{{myDate | date: 'yyyy'}}</ion-label>
第一个只是 ts 文件中的导入,第二个是您放入 html 文件中。
我正在从 sqlite 数据库循环获取日期。格式类似于 "Wed Feb 01 2017 21:54:24 GMT-0200"。但我想将其更改为 'dd/MM/yyyy'。
如何在控制器中更改格式?
例如我的代码:
this.issuedService.fetchAll().then(( res ) => {
var data = [];
for (let entry of <Array<any>>res) {
console.log( JSON.stringify( entry ) );
}
//this.items = <Array<any>>res;
}, ( error ) => {
console.log( "ERROR: ", error.message );
});
更新 2017-02-02
于是,几个小时没睡觉,我终于找到了解决办法。
我可以从 NPM 将 MomentJs 库添加到我的 Ionic v2 项目中。
$ npm install moment --save
结果代码:
import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import * as moment from 'moment';
import 'moment/locale/pt-br';
import { IssuedService } from '../../providers/issued-service';
@Component( {
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
items: any[] = [];
issuedPage = IssuedPage;
constructor(
public navCtrl: NavController,
public navParams: NavParams,
public issuedService: IssuedService
) {
this.initializeItems();
}
ionViewDidLoad() {
console.log( 'ionViewDidLoad HomePage' );
}
initializeItems() {
this.issuedService.fetchAll().then(( res ) => {
for ( let entry of <Array<any>>res ) {
entry.query_dt = moment( entry.picking_dt ).format( 'DD MMMM YYYY' );
this.items.push(entry);
}
console.log( JSON.stringify( this.items ) );
}, ( error ) => {
console.log( "ERROR: ", error.message );
});
}
}
试试这个
$scope.date = 'Wed Feb 01 2017 21:54:24 GMT-0200';
var filterdatetime = $filter('date')( $scope.date,'dd/MM/yyyy' );
alert(filterdatetime);
我项目的解决方案:
Adding third party libs
You can add most third party libraries to your V2 project from NPM. For example let’s add MomentJs.
$ npm install moment --save
From here, we can import it into what ever class we want to use it in.
import {Page} from 'ionic-angular';
import * as moment from 'moment';
export class MyClass {
constructor(){
moment("20111031", "YYYYMMDD").fromNow();
}
}
我用两行做到了:
import { DatePipe } from '@angular/common';
<ion-label>{{myDate | date: 'yyyy'}}</ion-label>
第一个只是 ts 文件中的导入,第二个是您放入 html 文件中。