moodle_local_treestudyplan/amd/build/util/date-helper.min.js.map

1 line
8.7 KiB
Plaintext
Raw Normal View History

2024-06-03 23:24:16 +02:00
{"version":3,"file":"date-helper.min.js","sources":["../../src/util/date-helper.js"],"sourcesContent":["/* eslint capitalized-comments: \"off\" */\n/**\n * Format a date according to localized custom\n * @param {Date|string} d The date to convert\n * @param {boolean} short Short format (default false)\n * @returns {string}\n */\nexport function formatDate(d, short) {\n if (!(d instanceof Date)) {\n if (typeof d == 'number') {\n d *= 1000; // Convert from seconds to milliseconds.\n }\n d = new Date(d);\n }\n\n let monthformat = \"short\";\n if (short === true) {\n monthformat = \"numeric\";\n } else if (short === false) {\n monthformat = \"long\";\n }\n return d.toLocaleDateString(document.documentElement.lang, {\n year: 'numeric', month: monthformat, day: 'numeric'\n });\n}\n\n/**\n * Format a date according to localized custom\n * @param {Date|string} d The date to convert\n * @param {boolean} short Short format (default false)\n * @returns {string}\n */\nexport function formatDatetime(d, short) {\n if (!(d instanceof Date)) {\n if (typeof d == 'number') {\n d *= 1000; // Convert from seconds to milliseconds.\n }\n d = new Date(d);\n }\n\n let monthformat = \"short\";\n if (short === true) {\n monthformat = \"numeric\";\n } else if (short === false) {\n monthformat = \"long\";\n }\n return d.toLocaleDateString(document.documentElement.lang, {\n year: 'numeric', month: monthformat, day: 'numeric'\n }) + \" \" + d.toLocaleTimeString(document.documentElement.lang, {\n timeStyle: \"short\",\n });\n}\n\n/**\n * Provides standardized information about the period between two dates\n * As\n * @param {Date|string} first First day of the span\n * @param {Date|string} last Last day of the span\n * @returns {Object} Object containing formatted start and end dates and span information\n */\nexport function datespaninfo(first, last) {\n if (!(first instanceof Date)) {\n first = new Date(first);\n }\n if (!(last instanceof Date)) {\n last = new Date(last);\n }\n\n // Make sure the end date is at the very end of the day and the start date at the very beginning\n first.setHours(0);\n first.setMinutes(0);\n first.setSeconds(0);\n first.setMilliseconds(0);\n last.setHours(23);\n last.setMinutes(59);\n last.setSeconds(59);\n last.setMilliseconds(999);\n\n const dayspan = Math.round(((last - first) + 1) / (24 * 60 * 60 * 1000)); // Add one millisecond to offset the 999 ms\n const years = Math.floor(dayspan / 365); // Yes, we ignore leap years/leap days\n const ydaysleft = dayspan % 365;\n\n const weeks = Math.floor(ydaysleft / 7);\n const wdaysleft = ydaysleft % 7;\n\n return {\n first: first,\n last: last,\n totaldays: dayspan,\n years: years,\n weeks: weeks,\n days: wdaysleft,\n formatted: {\n first: formatDate(first),\n last: formatDate(last),\n }\n };\n\n}\n\n/**\n * Format a Date object to YYYY-MM-DD format\n * @param {Date} date Date object\n * @returns {String} Date string in YYYY-MM-DD format\n */\nfunction dateYYYYMMDD(date) {\n const d = new Date(date);\n let month = '' + (d.getMonth() + 1);\n let day = '' + d.getDate();\n const year = d.getFullYear();\n\n if (month.length < 2) {\n month = '0' + month;\n }\n if (day.length < 2) {\n day = '0' + day;\n }\n return [year, month, day].join('-');\n}\n\n/**\n * String formatting function - replaces {name} in string by value of same key in values parameter\n * @param {string} datestr String containing date format in YYYY-MM-DD\n * @param {int} days Object containing keys to replace {key} strings with. Make negative to subtract\n * @returns {String} Date string in YYYY-MM-DD format\n */\nexport function addDays(datestr, days) {\n const date = new Date(datestr);\n const newdate = new Date(date.getTime() + (days * 86400000)); // Add n