{"version":3,"file":"date-helper.min.js","sources":["../../src/util/date-helper.js"],"sourcesContent":["/**\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 format_date(d,short){\n if(!(d instanceof Date)){\n d = new Date(d);\n }\n\n let monthformat = \"short\";\n if(short){\n monthformat = \"numeric\";\n }\n return d.toLocaleDateString(document.documentElement.lang,{\n year: 'numeric', month: monthformat, day: 'numeric'\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)){ first = new Date(first);}\n if(!(last instanceof Date)){ last = new Date(last);}\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: format_date(first),\n last: format_date(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 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 Date string in YYYY-MM-DD format\n */\nexport function add_days(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\n */\nexport function studyplanDates(plan) {\n let earliestStart = null;\n let latestEnd = null;\n let openEnded = false;\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 const dates