1 line
8.5 KiB
Plaintext
1 line
8.5 KiB
Plaintext
{"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 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 format_datetime(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)){ 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\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":["format_date","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":"uFAMgBA,YAAYC,EAAEC,OACrBD,aAAaE,OACE,iBAALF,IACPA,GAAK,KAETA,EAAI,IAAIE,KAAKF,QAGbG,YAAc,eACL,IAAVF,MACCE,YAAc,WACG,IAAVF,QACPE,YAAc,QAEXH,EAAEI,mBAAmBC,SAASC,gBAAgBC,KAAK,CACtDC,KAAM,UAAWC,MAAON,YAAaO,IAAK,qBA+GlCC,eAAeC,UACvBC,cAAgB,KAChBC,UAAY,KACZC,WAAY,MAEZ,MAAMC,MAAMJ,KAAKK,MAAO,OAClBC,KAAON,KAAKK,MAAMD,IAClBG,EAAI,IAAIjB,KAAKgB,KAAKE,cAClBF,KAAKG,UACPN,WAAY,KAGXF,eAAiBM,EAAIN,iBACtBA,cAAgBM,GAGfD,KAAKG,QAAU,OACVC,EAAI,IAAIpB,KAAKgB,KAAKG,WACnBP,WAAaQ,EAAIR,aACl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