【问题标题】:How to format date in moment js如何在时刻 js 中格式化日期
【发布时间】:2020-10-08 08:04:14
【问题描述】:

我们如何从时刻 js 自定义日期?我只想显示 Ex: 本月的 Thu 10。 我们如何使用 moment 或 pipe 来实现这一点需要您的帮助。

app.component.html

<h2>{{ dateCtr.value | date }}</h2>

app.component.ts

import { Component } from '@angular/core';
import {FormControl} from '@angular/forms';
import {MomentDateAdapter, MAT_MOMENT_DATE_ADAPTER_OPTIONS} from '@angular/material-moment-adapter';
import {DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE} from '@angular/material/core';

import _moment from 'moment';
// tslint:disable-next-line:no-duplicate-imports
import {default as _rollupMoment} from 'moment';

const moment = _rollupMoment || _moment;

// See the Moment.js docs for the meaning of these formats:
// https://momentjs.com/docs/#/displaying/format/
export const MY_FORMATS = {
  parse: {
    dateInput: 'LL',
  },
  display: {
    dateInput: 'LL',
    monthYearLabel: 'MMM YYYY',
    dateA11yLabel: 'LL',
    monthYearA11yLabel: 'MMMM YYYY',
  },
};

@Component({
  selector: 'my-app',
  templateUrl: './app.component.html',
  styleUrls: [ './app.component.css' ],
  providers: [
    // `MomentDateAdapter` can be automatically provided by importing `MomentDateModule` in your
    // application's root module. We provide it at the component level here, due to limitations of
    // our example generation script.
    {
      provide: DateAdapter,
      useClass: MomentDateAdapter,
      deps: [MAT_DATE_LOCALE, MAT_MOMENT_DATE_ADAPTER_OPTIONS]
    },

    {provide: MAT_DATE_FORMATS, useValue: MY_FORMATS},
  ],
})

export class AppComponent  {

  dateCtr = new FormControl(moment());

  incrementDate(){
  this.dateCtr.setValue(moment(this.dateCtr.value).add(1, 'days').format());
 }

  decrementDate(){
  this.dateCtr.setValue(moment(this.dateCtr.value).subtract(1, 'days').format());
 }

}

管道.ts

import { Pipe, PipeTransform } from '@angular/core';
import { DatePipe } from '@angular/common';

@Pipe({ name: 'mypipe' })
export class Mypipe implements PipeTransform {
  // adding a default value in case you don't want to pass the format then 'yyyy-MM-dd' will be used
  transform(date: Date | string, day: number, format: string = 'yyyy-MM-dd'): string {
    date = new Date(date);  // if orginal type was a string
    date.setDate(date.getDate() - day);
    return new DatePipe('en-US').transform(date, format);
  }
}

我们如何从时刻 js 自定义日期?我只想显示 Ex: 本月的 Thu 10。 我们如何使用 moment 或 pipe 来实现这一点需要您的帮助。

【问题讨论】:

    标签: angular pipe momentjs


    【解决方案1】:

    使用moment可以通过this来实现

    moment(datavalue).format('ddd DD [of] MMMM')
    

    我认为这将解决您的问题。

    【讨论】:

      【解决方案2】:

      第 1 步:从“@angular/comman”导入 { Datepipe }; // 在 app.modules.ts 中导入

      第 2 步:使用

      {{ dateCtr.value |日期:“EE d”}}

      而不是

      {{ dateCtr.value |日期}}

      【讨论】:

        【解决方案3】:

        不用momentJs也可以做到。我就是这样做的:
        Typescript:

        const days = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
        const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
        const date = new Date();
        const dayName = this.days[this.date.getDay()];
        const monthName = this.months[this.date.getMonth()];
        

        HTML:

        <p>{{ dayName }} {{ date | date:'dd' }} {{ monthName }} {{ date | date:'yyyy' }}</p>
        

        输出: 2020 年 10 月 8 日星期二

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-10-02
          • 2020-01-14
          • 2021-09-15
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多