UNPKG

@microsoft/windows-admin-center-sdk

Version:

Microsoft - Windows Admin Center Shell

173 lines (171 loc) 6.58 kB
export var DateTime; (function (DateTime) { DateTime.DateTimeFormatCharacter = { Year: 'y', Month: 'M', Day: 'd' }; function copyDate(src, dest) { const date = new Date(src.getFullYear(), src.getMonth(), src.getDate(), dest.getHours(), dest.getMinutes(), dest.getSeconds(), dest.getMilliseconds()); dest.setTime(date.getTime()); } DateTime.copyDate = copyDate; function copyTime(src, dest) { const date = new Date(dest.getFullYear(), dest.getMonth(), dest.getDate(), src.getHours(), src.getMinutes(), src.getSeconds(), src.getMilliseconds()); dest.setTime(date.getTime()); } DateTime.copyTime = copyTime; function isEqual(d1, d2) { const d1IsNull = MsftSme.isNullOrUndefined(d1); const d2IsNull = MsftSme.isNullOrUndefined(d2); if (d1 === d2 || (d1IsNull && d2IsNull)) { return true; } if (d1IsNull !== d2IsNull) { return false; } return d1.getTime() === d2.getTime(); } DateTime.isEqual = isEqual; function isEqualDates(d1, d2) { const d1IsNull = MsftSme.isNullOrUndefined(d1); const d2IsNull = MsftSme.isNullOrUndefined(d2); if (d1 === d2 || (d1IsNull && d2IsNull)) { return true; } if (d1IsNull !== d2IsNull) { return false; } return d1.getFullYear() === d2.getFullYear() && d1.getMonth() === d2.getMonth() && d1.getDate() === d2.getDate(); } DateTime.isEqualDates = isEqualDates; function isEqualTimes(d1, d2) { const d1IsNull = MsftSme.isNullOrUndefined(d1); const d2IsNull = MsftSme.isNullOrUndefined(d2); if (d1 === d2 || (d1IsNull && d2IsNull)) { return true; } if (d1IsNull !== d2IsNull) { return false; } return d1.getHours() === d2.getHours() && d1.getMinutes() === d2.getMinutes() && d1.getSeconds() === d2.getSeconds() && d1.getUTCMilliseconds() === d2.getUTCMilliseconds(); } DateTime.isEqualTimes = isEqualTimes; function compareDateTimes(d1, d2) { const d1IsNull = MsftSme.isNullOrUndefined(d1); const d2IsNull = MsftSme.isNullOrUndefined(d2); if (d1 === d2 || (d1IsNull && d2IsNull)) { return 0; } if (d1IsNull && !d2IsNull) { return -1; } if (!d1IsNull && d2IsNull) { return 1; } return d1 > d2 ? 1 : d1 < d2 ? -1 : 0; } DateTime.compareDateTimes = compareDateTimes; function compareDates(d1, d2) { const d1IsNull = MsftSme.isNullOrUndefined(d1); const d2IsNull = MsftSme.isNullOrUndefined(d2); if (d1 === d2 || (d1IsNull && d2IsNull)) { return 0; } if (d1IsNull && !d2IsNull) { return -1; } if (!d1IsNull && d2IsNull) { return 1; } const d1DateOnly = DateTime.getDateOnly(d1); const d2DateOnly = DateTime.getDateOnly(d2); return DateTime.compareDateTimes(d1DateOnly, d2DateOnly); } DateTime.compareDates = compareDates; function compareTimes(d1, d2) { const d1IsNull = MsftSme.isNullOrUndefined(d1); const d2IsNull = MsftSme.isNullOrUndefined(d2); if (d1 === d2 || (d1IsNull && d2IsNull)) { return 0; } if (d1IsNull && !d2IsNull) { return -1; } if (!d1IsNull && d2IsNull) { return 1; } const d1TimeOnly = DateTime.getTimeOnly(d1); const d2TimeOnly = DateTime.getTimeOnly(d2); return DateTime.compareDateTimes(d1TimeOnly, d2TimeOnly); } DateTime.compareTimes = compareTimes; function getDateOnly(date) { return new Date(date.getFullYear(), date.getMonth(), date.getDate()); } DateTime.getDateOnly = getDateOnly; function getTimeOnly(date, includeSeconds = false, includeMilliseconds = false) { if (includeMilliseconds) { return new Date(0, 0, 0, date.getHours(), date.getMinutes(), date.getSeconds(), date.getMilliseconds()); } else if (includeSeconds) { return new Date(0, 0, 0, date.getHours(), date.getMinutes(), date.getSeconds(), 0); } else { return new Date(0, 0, 0, date.getHours(), date.getMinutes(), 0, 0); } } DateTime.getTimeOnly = getTimeOnly; function getLastDayOfMonth(year, month) { return new Date(year, month + 1, 0); } DateTime.getLastDayOfMonth = getLastDayOfMonth; function getLastDayOfPreviousMonth(year, month) { const date = new Date(year, month, 1); date.setDate(date.getDate() - 1); return date; } DateTime.getLastDayOfPreviousMonth = getLastDayOfPreviousMonth; function getFirstDayOfNextMonth(year, month) { const date = new Date(year, month, this.getLastDayOfMonth(year, month).getDate()); date.setDate(date.getDate() + 1); return date; } DateTime.getFirstDayOfNextMonth = getFirstDayOfNextMonth; function getLocaleDateStringFormat(localeId) { let FormatPartType; (function (FormatPartType) { FormatPartType["Day"] = "day"; FormatPartType["Month"] = "month"; FormatPartType["Year"] = "year"; FormatPartType["Literal"] = "literal"; })(FormatPartType || (FormatPartType = {})); let dateFormat = ''; const formatter = new Intl.DateTimeFormat(localeId); const sampleDate = new Date('2/2/2020'); const parts = formatter.formatToParts(sampleDate); for (const part of parts) { switch (part.type) { case FormatPartType.Literal: dateFormat += part.value; break; case FormatPartType.Day: dateFormat += DateTime.DateTimeFormatCharacter.Day.repeat(part.value.length); break; case FormatPartType.Month: dateFormat += DateTime.DateTimeFormatCharacter.Month.repeat(part.value.length); break; case FormatPartType.Year: dateFormat += DateTime.DateTimeFormatCharacter.Year.repeat(part.value.length); } } return dateFormat; } DateTime.getLocaleDateStringFormat = getLocaleDateStringFormat; })(DateTime || (DateTime = {})); //# sourceMappingURL=date-time.js.map