UNPKG

@mikezimm/fps-core-v7

Version:

Library of reusable core interfaces, types and constants migrated from fps-library-v2

105 lines (104 loc) 4.23 kB
/** * CodeAnalizerComment: Updated 8 imports on 2024-09-21 23:07:24 * Update:: import { holidays } to '@mikezimm/fps-core-v7/lib/logic/Time/constants;' * Update:: import { msPerDay } to '@mikezimm/fps-core-v7/lib/logic/Time/constants;' * Update:: import { msPerHr } to '@mikezimm/fps-core-v7/lib/logic/Time/constants;' * Update:: import { msPerMin } to '@mikezimm/fps-core-v7/lib/logic/Time/constants;' * Update:: import { msPerMo } to '@mikezimm/fps-core-v7/lib/logic/Time/constants;' * Update:: import { msPerQ } to '@mikezimm/fps-core-v7/lib/logic/Time/constants;' * Update:: import { msPerWk } to '@mikezimm/fps-core-v7/lib/logic/Time/constants;' * Update:: import { msPerYr } to '@mikezimm/fps-core-v7/lib/logic/Time/constants;' */ import { msPerDay, } from './constants'; /** * This was built for TrackMyTime and will get the Sunday or Monday of the week of 'd'. * It returns the prior Sunday or prior Monday. * * Similarly getOffSetDayOfWeek can do this but it can uses different calculation * * Based upon: https://stackoverflow.com/questions/4156434/javascript-get-the-first-day-of-the-week-from-current-date * * @param d * @param sunOrMon */ export function getDayOfWeek(d, sunOrMon) { let d1 = new Date(d); let diff; let day = d1.getDay(); if (sunOrMon === 'sun') { diff = d1.getDate() - day; } else { diff = d1.getDate() - day + (day == 0 ? -6 : 1); // adjust when day is sunday } let newDate = new Date(d1.setDate(diff)); let returnDate = new Date(newDate.getFullYear(), newDate.getMonth(), newDate.getDate()); // console.log('getDayOfWeek:', d, sunOrMon,newDate ); // var day = d.getDay(), // diff = d.getDate() - day + (day == 0 ? -6:1); // adjust when day is sunday return returnDate; } //https://www.w3resource.com/javascript-exercises/javascript-date-exercise-24.php export function ISO8601_week_no(dt) { var tdt = new Date(dt.valueOf()); var dayn = (dt.getDay() + 6) % 7; tdt.setDate(tdt.getDate() - dayn + 3); var firstThursday = tdt.valueOf(); tdt.setMonth(0, 1); if (tdt.getDay() !== 4) { tdt.setMonth(0, 1 + ((4 - tdt.getDay()) + 7) % 7); } return 1 + Math.ceil((firstThursday - tdt.valueOf()) / 604800000); } /** * Created for GridCharts * @param theDate */ export function getYearWeekLabel(theDate) { let year = theDate.getFullYear(); let weekNo = ISO8601_week_no(theDate).toString(); if (weekNo.length === 1) { weekNo = "0" + weekNo; } let weekLabel = year + ' : w' + weekNo; return weekLabel; } /** * This is similar to getDayOfWeek except more complex: * * Function does not adjust the timestamp. * So if the day number is the same even if it's earlier or later, it will return the date you originally passed in. * * You can pass in any day number (0 for Sunday) and which = prior or next. * 0 = Sunday, 1 = Monday, 2 = Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday * * Which = prior: * Say today is Wednesday and you request day 2, that would return Tuesday (yesterday) * say today is Wednesday and you request day 4, that would return LAST Thursday because that is the first prior Thursday. * * Which = next: * Say today is Wednesday and you request day 2, that will return NEXT Tuesday * Say today is Wednesday and you request day 4, that will return tommorrow. * * Pass in a date, the day number to get and wether it's prior or future and it will get the date. * @param d * @param day * @param which */ //This will be in npmfunctions in v.0.0.5 export function getOffSetDayOfWeek(d, day, which) { //First get current day number of week let theDate = new Date(d); let dayOfWeek = theDate.getDay(); if (dayOfWeek === day) { return theDate; } else { let deltaDays = which === 'prior' ? -dayOfWeek : 7 - dayOfWeek; let deltaMS = deltaDays * msPerDay; let adjustedTime = theDate.getTime() + deltaMS; let adjustedDate = new Date(adjustedTime); return adjustedDate; } } //# sourceMappingURL=weeks.js.map