如何解决如何在Moment JS中格式化日期
我们如何自js时刻起自定义日期?我只想显示该月的Ex:周四10 。 我们如何才能实现使用力矩或管道需要您的帮助。
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());
}
}
pipe.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:周四10 。 我们如何才能实现使用力矩或管道需要您的帮助。
解决方法
这可以立即完成。这是我的方法:
打字稿:
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日星期二
,第1步:从“ @ angular / comman”导入{Datepipe}; //导入app.modules.ts内部
步骤2:使用
{{dateCtr.value |日期:“ EE d”}}
代替
{{dateCtr.value |日期}}
,
使用此刻您可以实现它
moment(datavalue).format('ddd DD [of] MMMM')
我认为这可以解决您的问题。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。