javascript - momentjs datetime format on frontend with angular - Stack Overflow

I'm using currently moment.js in my project. I want to remove "T" and +02:00. There must

I'm using currently moment.js in my project. I want to remove "T" and +02:00. There must be a date and time only. But if I use the .format() method of moment.js I get the default datetime.

I want to format this datetime:

from ' 2022-02-11T04:20:13+02:00 ' to ' 2022-02-11 04:20:13 '

back

import * as moment from 'moment';

date_times: any;
constructor() {
this.date_times =  moment().format('YYYY-MM-DD HH:mm:ss');
}

front

 <ion-item>
  <ion-label>Select date & time</ion-label>
  <ion-datetime displayFormat="D MMM YYYY H:mm A" (ionChange)="showdate()" [(ngModel)]="date_times"></ion-datetime>
</ion-item>

{{date_times }}

I'm using currently moment.js in my project. I want to remove "T" and +02:00. There must be a date and time only. But if I use the .format() method of moment.js I get the default datetime.

I want to format this datetime:

from ' 2022-02-11T04:20:13+02:00 ' to ' 2022-02-11 04:20:13 '

back

import * as moment from 'moment';

date_times: any;
constructor() {
this.date_times =  moment().format('YYYY-MM-DD HH:mm:ss');
}

front

 <ion-item>
  <ion-label>Select date & time</ion-label>
  <ion-datetime displayFormat="D MMM YYYY H:mm A" (ionChange)="showdate()" [(ngModel)]="date_times"></ion-datetime>
</ion-item>

{{date_times }}
Share Improve this question asked Feb 9, 2022 at 2:37 MarcusMarcus 571 gold badge2 silver badges13 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

moment().format('YYYY-MM-DD HH:mm:ss') will give you the format you want but since you are using date_times as the ngModel of <ion-datetime> ponent, its value has been changed after you initialized the value in the constructor().

You can format date_times when you print it out by using Pipe like this:

my-datetime-format.pipe.ts:

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

@Pipe({
  name: 'myDateTimeFormat'
})
export class myDateTimeFormatPipe implements PipeTransform {
  transform(value: string): string {
    return moment(value).format('YYYY-MM-DD HH:mm:ss');
  }
}

In template:

{{ date_times | myDateTimeFormat }}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742348408a4427013.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信