UNPKG

devextreme

Version:

JavaScript/TypeScript Component Suite for Responsive Web Development

206 lines (204 loc) • 7.65 kB
/** * DevExtreme (cjs/__internal/ui/date_box/date_utils.js) * Version: 25.2.5 * Build date: Fri Feb 20 2026 * * Copyright (c) 2012 - 2026 Developer Express Inc. ALL RIGHTS RESERVED * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/ */ "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _date = _interopRequireDefault(require("../../../common/core/localization/date")); var _renderer = _interopRequireDefault(require("../../../core/renderer")); var _date_serialization = _interopRequireDefault(require("../../../core/utils/date_serialization")); var _type = require("../../../core/utils/type"); function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e } } const DATE_COMPONENTS = ["year", "day", "month", "day"]; const TIME_COMPONENTS = ["hours", "minutes", "seconds", "milliseconds"]; const ONE_MINUTE = 6e4; const ONE_DAY = 864e5; const ONE_YEAR = 31536e6; const getStringFormat = format => { const formatType = typeof format; if ("string" === formatType) { return "format" } if ("object" === typeof format && void 0 !== format.type) { return format.type } return null }; const dateUtils = { SUPPORTED_FORMATS: ["date", "time", "datetime"], ONE_MINUTE: 6e4, ONE_DAY: ONE_DAY, ONE_YEAR: ONE_YEAR, MIN_DATEVIEW_DEFAULT_DATE: new Date(1900, 0, 1), MAX_DATEVIEW_DEFAULT_DATE: (() => { const newDate = new Date; return new Date(newDate.getFullYear() + 50, newDate.getMonth(), newDate.getDate(), 23, 59, 59) })(), FORMATS_INFO: { date: { getStandardPattern: () => "yyyy-MM-dd", components: DATE_COMPONENTS }, time: { getStandardPattern: () => "HH:mm", components: TIME_COMPONENTS }, datetime: { getStandardPattern() { let standardPattern = "yyyy-MM-ddTHH:mm:ssZ"; ! function() { const $input = (0, _renderer.default)("<input>").attr("type", "datetime"); $input.val("2000-01-01T01:01Z"); if ($input.val()) { standardPattern = "yyyy-MM-ddTHH:mmZ" } }(); standardPattern ?? (standardPattern = "yyyy-MM-ddTHH:mm:ssZ"); dateUtils.FORMATS_INFO.datetime.getStandardPattern = () => standardPattern; return standardPattern }, components: [...DATE_COMPONENTS, ...TIME_COMPONENTS] }, "datetime-local": { getStandardPattern: () => "yyyy-MM-ddTHH:mm:ss", components: [...DATE_COMPONENTS, "hours", "minutes", "seconds"] } }, FORMATS_MAP: { date: "shortdate", time: "shorttime", datetime: "shortdateshorttime" }, SUBMIT_FORMATS_MAP: { date: "date", time: "time", datetime: "datetime-local" }, toStandardDateFormat(date, type) { const pattern = dateUtils.FORMATS_INFO[type].getStandardPattern(); return _date_serialization.default.serializeDate(date, pattern) }, fromStandardDateFormat(text) { const date = _date_serialization.default.dateParser(text); return (0, _type.isDate)(date) ? date : void 0 }, getMaxMonthDay: (year, month) => new Date(year, month + 1, 0).getDate(), mergeDates(oldValue, newValue, format) { let oldDate = oldValue; if (!newValue) { return newValue ?? null } if (!oldDate || isNaN(oldDate.getTime())) { const now = new Date(0); oldDate = new Date(now.getFullYear(), now.getMonth(), now.getDate()) } const result = new Date(oldDate.valueOf()); const formatInfo = dateUtils.FORMATS_INFO[format]; formatInfo.components.forEach((component => { const componentInfo = dateUtils.DATE_COMPONENTS_INFO[component]; result[componentInfo.setter](newValue[componentInfo.getter]()) })); return result }, getLongestCaptionIndex(captionArray) { let longestIndex = 0; let longestCaptionLength = 0; for (let i = 0; i < captionArray.length; i += 1) { if (captionArray[i].length > longestCaptionLength) { longestIndex = i; longestCaptionLength = captionArray[i].length } } return longestIndex }, formatUsesMonthName: format => _date.default.formatUsesMonthName(format), formatUsesDayName: format => _date.default.formatUsesDayName(format), getLongestDate(format, monthNames, dayNames) { const stringFormat = getStringFormat(format); let month = 9; if (!stringFormat || dateUtils.formatUsesMonthName(stringFormat)) { month = dateUtils.getLongestCaptionIndex(monthNames) } const longestDate = new Date(1888, month, 21, 23, 59, 59, 999); if (!stringFormat || dateUtils.formatUsesDayName(stringFormat)) { const date = longestDate.getDate() - longestDate.getDay() + dateUtils.getLongestCaptionIndex(dayNames); longestDate.setDate(date) } return longestDate }, normalizeTime(date) { date.setSeconds(0); date.setMilliseconds(0) }, DATE_COMPONENTS_INFO: { year: { getter: "getFullYear", setter: "setFullYear", formatter(value, date) { const formatDate = new Date(date.getTime()); formatDate.setFullYear(value); return _date.default.format(formatDate, "yyyy") }, startValue: void 0, endValue: void 0 }, day: { getter: "getDate", setter: "setDate", formatter(value, date) { const formatDate = new Date(date.getTime()); formatDate.setDate(value); return _date.default.format(formatDate, "d") }, startValue: 1, endValue: void 0 }, month: { getter: "getMonth", setter: "setMonth", formatter: value => _date.default.getMonthNames()[value], startValue: 0, endValue: 11 }, hours: { getter: "getHours", setter: "setHours", formatter: value => _date.default.format(new Date(0, 0, 0, value), "hour"), startValue: 0, endValue: 23 }, minutes: { getter: "getMinutes", setter: "setMinutes", formatter: value => _date.default.format(new Date(0, 0, 0, 0, value), "minute"), startValue: 0, endValue: 59 }, seconds: { getter: "getSeconds", setter: "setSeconds", formatter: value => _date.default.format(new Date(0, 0, 0, 0, 0, value), "second"), startValue: 0, endValue: 59 }, milliseconds: { getter: "getMilliseconds", setter: "setMilliseconds", formatter: value => _date.default.format(new Date(0, 0, 0, 0, 0, 0, value), "millisecond"), startValue: 0, endValue: 999 } } }; var _default = exports.default = dateUtils;