1 line
8.7 KiB
Plaintext
1 line
8.7 KiB
Plaintext
{"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 days in ms.\n return dateYYYYMMDD(newdate);\n}\n\n/**\n * Determine start and end dates of a studyplan\n * @param {*} plan\n * @returns {object}\n */\nexport function studyplanDates(plan) {\n let earliestStart = null;\n let latestEnd = null;\n let openEnded = false;\n\n for (const ix in plan.pages) {\n const page = plan.pages[ix];\n const s = new Date(page.startdate);\n if (!page.enddate) {\n openEnded = true; // One of the pages has no end date set...\n }\n\n if (!earliestStart || s < earliestStart) {\n earliestStart = s;\n }\n\n if (page.enddate) {\n const e = new Date(page.enddate);\n if (!latestEnd || e > latestEnd) {\n latestEnd = e;\n }\n }\n }\n return {\n start: earliestStart,\n end: (openEnded) ? null : latestEnd,\n };\n}\n\n/**\n * Determine past/present/future for a studyplan\n * @param {*} plan\n * @returns {string} one of 'past', 'present', 'future'\n */\nexport function studyplanTiming(plan) {\n const now = new Date().getTime();\n\n // If timeless or no timing info is available, all plans are present\n if (!plan.pages && plan.pages.length == 0 || (plan.pages[0] && plan.pages[0].timeless)) {\n return 'present';\n }\n\n const dates = studyplanDates(plan);\n\n if (dates.start < now) {\n if (dates.end && now > dates.end) {\n return 'past';\n } else {\n return 'present';\n }\n } else {\n return 'future';\n }\n}\n\n/**\n * Determine past/present/future for a studyplan\n * @param {*} page\n * @returns {string} one of 'past', 'present', 'future'\n */\nexport function studyplanPageTiming(page) {\n const now = new Date().getTime();\n\n if (page.timeless) {\n return \"present\";\n }\n const start = new Date(page.startdate);\n const end = (page.enddate) ? (new Date(page.enddate)) : null;\n\n if (start < now) {\n if (end && now > end) {\n return 'past';\n } else {\n return 'present';\n }\n } else {\n return 'future';\n }\n}"],"names":["formatDate","d","short","Date","monthformat","toLocaleDateString","document","documentElement","lang","year","month","day","studyplanDates","plan","earliestStart","latestEnd","openEnded","ix","pages","page","s","startdate","enddate","e","start","end","datestr","days","date","getMonth","getDate","getFullYear","length","join","dateYYYYMMDD","getTime","first","last","setHours","setMinutes","setSeconds","setMilliseconds","dayspan","Math","round","years","floor","ydaysleft","weeks","totaldays","formatted","toLocaleTimeString","timeStyle","now","timeless","dates"],"mappings":"uFAOgBA,WAAWC,EAAGC,OACpBD,aAAaE,OACC,iBAALF,IACPA,GAAK,KAETA,EAAI,IAAIE,KAAKF,QAGbG,YAAc,eACJ,IAAVF,MACAE,YAAc,WACG,IAAVF,QACPE,YAAc,QAEXH,EAAEI,mBAAmBC,SAASC,gBAAgBC,KAAM,CACvDC,KAAM,UAAWC,MAAON,YAAaO,IAAK,qBAmHlCC,eAAeC,UACvBC,cAAgB,KAChBC,UAAY,KACZC,WAAY,MAEX,MAAMC,MAAMJ,KAAKK,MAAO,OACnBC,KAAON,KAAKK,MAAMD,IAClBG,EAAI,IAAIjB,KAAKgB,KAAKE,cACnBF,KAAKG,UACNN,WAAY,KAGXF,eAAiBM,EAAIN,iBACtBA,cAAgBM,GAGhBD,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