Skip to content

Commit 77d7e28

Browse files
committed
refactor(time-picker): addressing comments #6482
1 parent 2071cc1 commit 77d7e28

File tree

8 files changed

+167
-1090
lines changed

8 files changed

+167
-1090
lines changed

projects/igniteui-angular/src/lib/date-common/util/date-time.util.ts

+46-52
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,51 @@ export abstract class DateTimeUtil {
106106
return dateTimeParts;
107107
}
108108

109+
public static getPartValue(value: Date, datePartInfo: DatePartInfo, partLength: number): string {
110+
let maskedValue;
111+
const datePart = datePartInfo.type;
112+
switch (datePart) {
113+
case DatePart.Date:
114+
maskedValue = value.getDate();
115+
break;
116+
case DatePart.Month:
117+
// months are zero based
118+
maskedValue = value.getMonth() + 1;
119+
break;
120+
case DatePart.Year:
121+
if (partLength === 2) {
122+
maskedValue = this.prependValue(
123+
parseInt(value.getFullYear().toString().slice(-2), 10), partLength, '0');
124+
} else {
125+
maskedValue = value.getFullYear();
126+
}
127+
break;
128+
case DatePart.Hours:
129+
if (datePartInfo.format.indexOf('h') !== -1) {
130+
maskedValue = this.prependValue(
131+
this.toTwelveHourFormat(value.getHours().toString()), partLength, '0');
132+
} else {
133+
maskedValue = value.getHours();
134+
}
135+
break;
136+
case DatePart.Minutes:
137+
maskedValue = value.getMinutes();
138+
break;
139+
case DatePart.Seconds:
140+
maskedValue = value.getSeconds();
141+
break;
142+
case DatePart.AmPm:
143+
maskedValue = value.getHours() >= 12 ? 'PM' : 'AM';
144+
break;
145+
}
146+
147+
if (datePartInfo.type !== DatePart.AmPm) {
148+
return this.prependValue(maskedValue, partLength, '0');
149+
}
150+
151+
return maskedValue;
152+
}
153+
109154
/** Builds a date-time editor's default input format based on provided locale settings. */
110155
public static getDefaultInputFormat(locale: string): string {
111156
locale = locale || DateTimeUtil.DEFAULT_LOCALE;
@@ -355,13 +400,7 @@ export abstract class DateTimeUtil {
355400
const dateNow = new Date().toISOString();
356401
// eslint-disable-next-line prefer-const
357402
let [datePart, timePart] = dateNow.split(timeLiteral);
358-
// transform the provided value to a numeric mask
359-
// and use the mask parser to update it with the value
360-
const format = timePart.replace(/\d/g, '0');
361-
timePart = new MaskParsingService().replaceInMask(timePart, value,
362-
{ format, promptChar: '' }, 0, value.length).value;
363-
timePart = timePart.substr(0, timePart.length - 1);
364-
return new Date(`${datePart}T${timePart}`);
403+
return new Date(`${datePart}T${value}`);
365404
}
366405

367406
return null;
@@ -428,51 +467,6 @@ export abstract class DateTimeUtil {
428467
console.warn('Using default browser locale settings.');
429468
}
430469

431-
public static getPartValue(value: Date, datePartInfo: DatePartInfo, partLength: number): string {
432-
let maskedValue;
433-
const datePart = datePartInfo.type;
434-
switch (datePart) {
435-
case DatePart.Date:
436-
maskedValue = value.getDate();
437-
break;
438-
case DatePart.Month:
439-
// months are zero based
440-
maskedValue = value.getMonth() + 1;
441-
break;
442-
case DatePart.Year:
443-
if (partLength === 2) {
444-
maskedValue = this.prependValue(
445-
parseInt(value.getFullYear().toString().slice(-2), 10), partLength, '0');
446-
} else {
447-
maskedValue = value.getFullYear();
448-
}
449-
break;
450-
case DatePart.Hours:
451-
if (datePartInfo.format.indexOf('h') !== -1) {
452-
maskedValue = this.prependValue(
453-
this.toTwelveHourFormat(value.getHours().toString()), partLength, '0');
454-
} else {
455-
maskedValue = value.getHours();
456-
}
457-
break;
458-
case DatePart.Minutes:
459-
maskedValue = value.getMinutes();
460-
break;
461-
case DatePart.Seconds:
462-
maskedValue = value.getSeconds();
463-
break;
464-
case DatePart.AmPm:
465-
maskedValue = value.getHours() >= 12 ? 'PM' : 'AM';
466-
break;
467-
}
468-
469-
if (datePartInfo.type !== DatePart.AmPm) {
470-
return this.prependValue(maskedValue, partLength, '0');
471-
}
472-
473-
return maskedValue;
474-
}
475-
476470
private static prependValue(value: number, partLength: number, prependChar: string): string {
477471
return (prependChar + value.toString()).slice(-partLength);
478472
}

0 commit comments

Comments
 (0)