- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
在我的 Angular 4 项目中,我使用了一些 matDatepicker ,如果我从日期选择器中选择一个日期,它可以正常工作,但如果我在输入字段中手动输入日期,我会在不正确的区域设置中看到日期。
我正在关注:github , 但我只在选择日期时看到正确的语言环境,而在我写日期时看不到。语言环境是否可能被覆盖?
这是日期适配器:
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {Inject, Injectable, Optional} from '@angular/core';
import {DateAdapter, MAT_DATE_LOCALE, MatDateFormats} from '@angular/material';
// Depending on whether rollup is used, moment needs to be imported differently.
// Since Moment.js doesn't have a default export, we normally need to import using the `* as`
// syntax. However, rollup creates a synthetic default module and we thus need to import it using
// the `default as` syntax.
// TODO(mmalerba): See if we can clean this up at some point.
import * as _moment from 'moment';
// import {default as _rollupMoment, Moment} from 'moment';
import 'moment/locale/fr';
import { Moment } from 'moment';
const moment = _moment; // _rollupMoment || _moment;
export const MOMENT_DATE_FORMATS: MatDateFormats = {
parse: {
dateInput: 'D/MM/YYYY'
},
display: {
dateInput: 'DD/MM/YYYY',
monthYearLabel: 'MMMM Y',
dateA11yLabel: 'LL',
monthYearA11yLabel: 'MMMM Y'
}
};
/** Creates an array and fills it with values. */
function range<T>(length: number, valueFunction: (index: number) => T): T[] {
const valuesArray = Array(length);
for (let i = 0; i < length; i++) {
valuesArray[i] = valueFunction(i);
}
return valuesArray;
}
/** Adapts Moment.js Dates for use with Angular Material. */
@Injectable()
export class MomentDateAdapter extends DateAdapter<Moment> {
// Note: all of the methods that accept a `Moment` input parameter immediately call `this.clone`
// on it. This is to ensure that we're working with a `Moment` that has the correct locale setting
// while avoiding mutating the original object passed to us. Just calling `.locale(...)` on the
// input would mutate the object.
private _localeData: {
firstDayOfWeek: number,
longMonths: string[],
shortMonths: string[],
dates: string[],
longDaysOfWeek: string[],
shortDaysOfWeek: string[],
narrowDaysOfWeek: string[]
};
constructor(@Optional() @Inject(MAT_DATE_LOCALE) dateLocale: string) {
super();
this.setLocale(dateLocale || moment.locale());
}
setLocale(locale: string) {
super.setLocale(locale);
let momentLocaleData = moment.localeData(locale);
this._localeData = {
firstDayOfWeek: momentLocaleData.firstDayOfWeek(),
longMonths: momentLocaleData.months(),
shortMonths: momentLocaleData.monthsShort(),
dates: range(31, (i) => this.createDate(2017, 0, i + 1).format('D')),
longDaysOfWeek: momentLocaleData.weekdays(),
shortDaysOfWeek: momentLocaleData.weekdaysShort(),
narrowDaysOfWeek: momentLocaleData.weekdaysMin(),
};
}
getYear(date: Moment): number {
return this.clone(date).year();
}
getMonth(date: Moment): number {
return this.clone(date).month();
}
getDate(date: Moment): number {
return this.clone(date).date();
}
getDayOfWeek(date: Moment): number {
return this.clone(date).day();
}
getMonthNames(style: 'long' | 'short' | 'narrow'): string[] {
// Moment.js doesn't support narrow month names, so we just use short if narrow is requested.
return style == 'long' ? this._localeData.longMonths : this._localeData.shortMonths;
}
getDateNames(): string[] {
return this._localeData.dates;
}
getDayOfWeekNames(style: 'long' | 'short' | 'narrow'): string[] {
if (style == 'long') {
return this._localeData.longDaysOfWeek;
}
if (style == 'short') {
return this._localeData.shortDaysOfWeek;
}
return this._localeData.narrowDaysOfWeek;
}
getYearName(date: Moment): string {
return this.clone(date).format('YYYY');
}
getFirstDayOfWeek(): number {
return this._localeData.firstDayOfWeek;
}
getNumDaysInMonth(date: Moment): number {
return this.clone(date).daysInMonth();
}
clone(date: Moment): Moment {
return date.clone().locale(this.locale);
}
createDate(year: number, month: number, date: number): Moment {
// Moment.js will create an invalid date if any of the components are out of bounds, but we
// explicitly check each case so we can throw more descriptive errors.
if (month < 0 || month > 11) {
throw Error(`Invalid month index "${month}". Month index has to be between 0 and 11.`);
}
if (date < 1) {
throw Error(`Invalid date "${date}". Date has to be greater than 0.`);
}
let result = moment({year, month, date}).locale(this.locale);
// If the result isn't valid, the date must have been out of bounds for this month.
if (!result.isValid()) {
throw Error(`Invalid date "${date}" for month with index "${month}".`);
}
return result;
}
today(): Moment {
return moment().locale(this.locale);
}
parse(value: any, parseFormat: string | string[]): Moment | null {
if (value && typeof value == 'string') {
return moment(value, parseFormat, this.locale);
}
return value ? moment(value).locale(this.locale) : null;
}
format(date: Moment, displayFormat: string): string {
date = this.clone(date);
if (!this.isValid(date)) {
throw Error('MomentDateAdapter: Cannot format invalid date.');
}
return date.format(displayFormat);
}
addCalendarYears(date: Moment, years: number): Moment {
return this.clone(date).add({years});
}
addCalendarMonths(date: Moment, months: number): Moment {
return this.clone(date).add({months});
}
addCalendarDays(date: Moment, days: number): Moment {
return this.clone(date).add({days});
}
toIso8601(date: Moment): string {
return this.clone(date).format();
}
/**
* Returns the given value if given a valid Moment or null. Deserializes valid ISO 8601 strings
* (https://www.ietf.org/rfc/rfc3339.txt) and valid Date objects into valid Moments and empty
* string into null. Returns an invalid date for all other values.
*/
deserialize(value: any): Moment | null {
let date;
if (value instanceof Date) {
date = moment(value);
}
if (typeof value === 'string') {
if (!value) {
return null;
}
date = moment(value, moment.ISO_8601).locale(this.locale);
}
if (date && this.isValid(date)) {
return date;
}
return super.deserialize(value);
}
isDateInstance(obj: any): boolean {
return moment.isMoment(obj);
}
isValid(date: Moment): boolean {
return this.clone(date).isValid();
}
invalid(): Moment {
return moment.invalid();
}
}
在我的app.module
import { MomentDateAdapter, MOMENT_DATE_FORMATS } from './shared/moment-date-adapter/moment-date-adapter';
{ provide: MAT_DATE_LOCALE, useValue: 'it-IT' },
{ provide: MAT_DATE_FORMATS, useValue: MOMENT_DATE_FORMATS },
{ provide: DateAdapter, useClass: MomentDateAdapter },
第一个是选择的日期,第二个是写的日期
最佳答案
我不知道你到底想在那里做什么,但你不需要这样的适配器来做到这一点......首先创建它:
export const CUSTOM_DATE_FORMAT = {
parse: {
dateInput: 'DD/MM/YYYY',
},
display: {
dateInput: 'DD/MM/YYYY',
monthYearLabel: 'MMMM YYYY',
dateA11yLabel: 'DD/MM/YYYY',
monthYearA11yLabel: 'MMMM YYYY',
},
};
然后,在你的模块中(fr是我的,随便替换):
import { MatMomentDateModule, MomentDateAdapter } from '@angular/material-moment-adapter';
imports: [MatMomentDateModule],
providers: [
{provide: DateAdapter, useClass: MomentDateAdapter},
{provide: MAT_DATE_FORMATS, useValue: CUSTOM_DATE_FORMAT},
{provide: MAT_DATE_LOCALE, useValue: 'fr-FR'}
]
关于angular - 如果用户在输入中写入日期,Material DatePicker 会错误解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48762645/
尝试使用集成到 QTCreator 的表单编辑器,但即使我将插件放入 QtCreator.app/Contents/MacOS/designer 也不会显示。不过,相同的 dylib 文件确实适用于独
在此代码示例中。 “this.method2();”之后会读到什么?在返回returnedValue之前会跳转到method2()吗? public int method1(int returnedV
我的项目有通过gradle配置的依赖项。我想添加以下依赖项: compile group: 'org.restlet.jse', name: 'org.restlet.ext.apispark', v
我将把我们基于 Windows 的客户管理软件移植到基于 Web 的软件。我发现 polymer 可能是一种选择。 但是,对于我们的使用,我们找不到 polymer 组件具有表格 View 、下拉菜单
我的项目文件夹 Project 中有一个文件夹,比如 ED 文件夹,当我在 Eclipse 中指定在哪里查找我写入的文件时 File file = new File("ED/text.txt"); e
这是奇怪的事情,这个有效: $('#box').css({"backgroundPosition": "0px 250px"}); 但这不起作用,它只是不改变位置: $('#box').animate
这个问题在这里已经有了答案: Why does OR 0 round numbers in Javascript? (3 个答案) 关闭 5 年前。 Mozilla JavaScript Guide
这个问题在这里已经有了答案: Is the function strcmpi in the C standard libary of ISO? (3 个答案) 关闭 8 年前。 我有一个问题,为什么
我目前使用的是共享主机方案,我不确定它使用的是哪个版本的 MySQL,但它似乎不支持 DATETIMEOFFSET 类型。 是否存在支持 DATETIMEOFFSET 的 MySQL 版本?或者有计划
研究 Seam 3,我发现 Seam Solder 允许将 @Named 注释应用于包 - 在这种情况下,该包中的所有 bean 都将自动命名,就好像它们符合条件一样@Named 他们自己。我没有看到
我知道 .append 偶尔会增加数组的容量并形成数组的新副本,但 .removeLast 会逆转这种情况并减少容量通过复制到一个新的更小的数组来改变数组? 最佳答案 否(或者至少如果是,则它是一个错
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
noexcept 函数说明符是否旨在 boost 性能,因为生成的对象中可能没有记录异常的代码,因此应尽可能将其添加到函数声明和定义中?我首先想到了可调用对象的包装器,其中 noexcept 可能会产
我正在使用 Angularjs 1.3.7,刚刚发现 Promise.all 在成功响应后不会更新 angularjs View ,而 $q.all 会。由于 Promises 包含在 native
我最近发现了这段JavaScript代码: Math.random() * 0x1000000 10.12345 10.12345 >> 0 10 > 10.12345 >>> 0 10 我使用
我正在编写一个玩具(物理)矢量库,并且遇到了 GHC 坚持认为函数应该具有 Integer 的问题。是他们的类型。我希望向量乘以向量以及标量(仅使用 * ),虽然这可以通过仅使用 Vector 来实现
PHP 的 mail() 函数发送邮件正常,但 Swiftmailer 的 Swift_MailTransport 不起作用! 这有效: mail('user@example.com', 'test
我尝试通过 php 脚本转储我的数据,但没有命令行。所以我用 this script 创建了我的 .sql 文件然后我尝试使用我的脚本: $link = mysql_connect($host, $u
使用 python 2.6.4 中的 sqlite3 标准库,以下查询在 sqlite3 命令行上运行良好: select segmentid, node_t, start, number,title
我最近发现了这段JavaScript代码: Math.random() * 0x1000000 10.12345 10.12345 >> 0 10 > 10.12345 >>> 0 10 我使用
我是一名优秀的程序员,十分优秀!