mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-08-23 14:01:20 +08:00
fix(datetime): fix am/pm in format w/out minutes or seconds
Closes #9269
This commit is contained in:
@ -278,7 +278,7 @@ export function updateDate(existingData: DateTimeData, newData: any) {
|
|||||||
|
|
||||||
|
|
||||||
export function parseTemplate(template: string): string[] {
|
export function parseTemplate(template: string): string[] {
|
||||||
let formats: string[] = [];
|
const formats: string[] = [];
|
||||||
|
|
||||||
template = template.replace(/[^\w\s]/gi, ' ');
|
template = template.replace(/[^\w\s]/gi, ' ');
|
||||||
|
|
||||||
@ -288,17 +288,19 @@ export function parseTemplate(template: string): string[] {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
let words = template.split(' ').filter(w => w.length > 0);
|
const words = template.split(' ').filter(w => w.length > 0);
|
||||||
words.forEach((word, i) => {
|
words.forEach((word, i) => {
|
||||||
FORMAT_KEYS.forEach(format => {
|
FORMAT_KEYS.forEach(format => {
|
||||||
if (word === format.f) {
|
if (word === format.f) {
|
||||||
if (word === FORMAT_A || word === FORMAT_a) {
|
if (word === FORMAT_A || word === FORMAT_a) {
|
||||||
// this format is an am/pm format, so it's an "a" or "A"
|
// this format is an am/pm format, so it's an "a" or "A"
|
||||||
|
console.log(`word: ${word}, words[i - 1]: ${words[i - 1]}`);
|
||||||
|
|
||||||
if ((formats.indexOf(FORMAT_h) < 0 && formats.indexOf(FORMAT_hh) < 0) ||
|
if ((formats.indexOf(FORMAT_h) < 0 && formats.indexOf(FORMAT_hh) < 0) ||
|
||||||
(words[i - 1] !== FORMAT_m && words[i - 1] !== FORMAT_mm)) {
|
VALID_AMPM_PREFIX.indexOf(words[i - 1]) === -1) {
|
||||||
// template does not already have a 12-hour format
|
// template does not already have a 12-hour format
|
||||||
// or this am/pm format doesn't have a minute format immediately before it
|
// or this am/pm format doesn't have a hour, minute, or second format immediately before it
|
||||||
// so do not treat this word "a" or "A" as an am/pm format
|
// so do not treat this word "a" or "A" as the am/pm format
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -515,3 +517,7 @@ const MONTH_SHORT_NAMES = [
|
|||||||
'Nov',
|
'Nov',
|
||||||
'Dec',
|
'Dec',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
const VALID_AMPM_PREFIX = [
|
||||||
|
FORMAT_hh, FORMAT_h, FORMAT_mm, FORMAT_m, FORMAT_ss, FORMAT_s
|
||||||
|
];
|
||||||
|
@ -321,6 +321,13 @@ describe('parseTemplate', () => {
|
|||||||
expect(formats[2]).toEqual('a');
|
expect(formats[2]).toEqual('a');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should allow am/pm when using only 12-hour', () => {
|
||||||
|
var formats = datetime.parseTemplate('hh a');
|
||||||
|
expect(formats.length).toEqual(2);
|
||||||
|
expect(formats[0]).toEqual('hh');
|
||||||
|
expect(formats[1]).toEqual('a');
|
||||||
|
});
|
||||||
|
|
||||||
it('should allow am/pm when using 12-hour', () => {
|
it('should allow am/pm when using 12-hour', () => {
|
||||||
var formats = datetime.parseTemplate('hh:mm a');
|
var formats = datetime.parseTemplate('hh:mm a');
|
||||||
expect(formats.length).toEqual(3);
|
expect(formats.length).toEqual(3);
|
||||||
@ -329,7 +336,7 @@ describe('parseTemplate', () => {
|
|||||||
expect(formats[2]).toEqual('a');
|
expect(formats[2]).toEqual('a');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should not add am/pm when not using 24-hour', () => {
|
it('should not add am/pm when using 24-hour', () => {
|
||||||
var formats = datetime.parseTemplate('HH:mm a');
|
var formats = datetime.parseTemplate('HH:mm a');
|
||||||
expect(formats.length).toEqual(2);
|
expect(formats.length).toEqual(2);
|
||||||
expect(formats[0]).toEqual('HH');
|
expect(formats[0]).toEqual('HH');
|
||||||
|
Reference in New Issue
Block a user