fix(datetime): do not set ampm when the column doesn't exist (#22224)

This commit is contained in:
Brandy Carney
2020-10-01 13:58:20 -04:00
committed by GitHub
parent 13168b6d77
commit 210db5b265
4 changed files with 51 additions and 9 deletions

View File

@@ -220,7 +220,7 @@ describe('datetime-util', () => {
"second": undefined,
"tzOffset": 0,
"year": 1000,
"ampm": "am"
"ampm": undefined
});
});
@@ -235,7 +235,7 @@ describe('datetime-util', () => {
"second": undefined,
"tzOffset": 0,
"year": undefined,
"ampm": "pm"
"ampm": undefined
});
});
@@ -250,7 +250,7 @@ describe('datetime-util', () => {
"second": 20,
"tzOffset": 0,
"year": 1994,
"ampm": "pm"
"ampm": undefined
});
});
@@ -265,7 +265,7 @@ describe('datetime-util', () => {
"second": undefined,
"tzOffset": 0,
"year": 2018,
"ampm": "am"
"ampm": undefined
});
});

View File

@@ -3,10 +3,16 @@
* Defaults to the current date if
* no date given
*/
export const getDateValue = (date: DatetimeData, format: string): number => {
export const getDateValue = (date: DatetimeData, format: string): number | string => {
const getValue = getValueFromFormat(date, format);
if (getValue !== undefined) { return getValue; }
if (getValue !== undefined) {
if (format === FORMAT_A || format === FORMAT_a) {
date.ampm = getValue;
}
return getValue;
}
const defaultDate = parseDate(new Date().toISOString());
return getValueFromFormat((defaultDate as DatetimeData), format);
@@ -238,7 +244,6 @@ export const parseDate = (val: string | undefined | null): DatetimeData | undefi
second: parse[6],
millisecond: parse[7],
tzOffset,
ampm: parse[4] >= 12 ? 'pm' : 'am'
};
};

View File

@@ -105,8 +105,8 @@
</ion-item>
<ion-item>
<ion-label>HH:mm</ion-label>
<ion-datetime display-format="HH:mm"></ion-datetime>
<ion-label>HH:mm A</ion-label>
<ion-datetime display-format="HH:mm A"></ion-datetime>
</ion-item>
<ion-item>
@@ -119,6 +119,11 @@
<ion-datetime display-format="h:mm a" value="01:47"></ion-datetime>
</ion-item>
<ion-item>
<ion-label>h:mm A</ion-label>
<ion-datetime display-format="h:mm A" value="14:23"></ion-datetime>
</ion-item>
<ion-item>
<ion-label>hh:mm A (15 min steps)</ion-label>
<ion-datetime display-format="h:mm A" minute-values="0,15,30,45"></ion-datetime>

View File

@@ -30,6 +30,38 @@ describe('Datetime', () => {
expect(monthvalue).toEqual(date.getMonth() + 1);
expect(yearValue).toEqual(date.getFullYear());
});
it('it should return the date value for a given time', () => {
const dateTimeData: DatetimeData = {
hour: 2,
minute: 23,
tzOffset: 0
};
const hourValue = getDateValue(dateTimeData, 'hh');
const minuteValue = getDateValue(dateTimeData, 'mm');
const ampmValue = getDateValue(dateTimeData, 'A');
expect(hourValue).toEqual(2);
expect(minuteValue).toEqual(23);
expect(ampmValue).toEqual("am");
});
it('it should return the date value for a given time after 12', () => {
const dateTimeData: DatetimeData = {
hour: 16,
minute: 47,
tzOffset: 0
};
const hourValue = getDateValue(dateTimeData, 'hh');
const minuteValue = getDateValue(dateTimeData, 'mm');
const ampmValue = getDateValue(dateTimeData, 'a');
expect(hourValue).toEqual(4);
expect(minuteValue).toEqual(47);
expect(ampmValue).toEqual("pm");
});
});
describe('getLocalDateTime()', () => {