@mikezimm/npmfunctions
Version:
Functions used in my SPFx webparts
100 lines • 3.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getOffSetDayOfWeek = exports.getYearWeekLabel = exports.ISO8601_week_no = exports.getDayOfWeek = void 0;
var constants_1 = require("./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
*/
function getDayOfWeek(d, sunOrMon) {
var d1 = new Date(d);
var diff;
var day = d1.getDay();
if (sunOrMon === 'sun') {
diff = d1.getDate() - day;
}
else {
diff = d1.getDate() - day + (day == 0 ? -6 : 1); // adjust when day is sunday
}
var newDate = new Date(d1.setDate(diff));
var 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;
}
exports.getDayOfWeek = getDayOfWeek;
//https://www.w3resource.com/javascript-exercises/javascript-date-exercise-24.php
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);
}
exports.ISO8601_week_no = ISO8601_week_no;
/**
* Created for GridCharts
* @param theDate
*/
function getYearWeekLabel(theDate) {
var year = theDate.getFullYear();
var weekNo = ISO8601_week_no(theDate).toString();
if (weekNo.length === 1) {
weekNo = "0" + weekNo;
}
var weekLabel = year + ' : w' + weekNo;
return weekLabel;
}
exports.getYearWeekLabel = getYearWeekLabel;
/**
* 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
function getOffSetDayOfWeek(d, day, which) {
//First get current day number of week
var theDate = new Date(d);
var dayOfWeek = theDate.getDay();
if (dayOfWeek === day) {
return theDate;
}
else {
var deltaDays = which === 'prior' ? -dayOfWeek : 7 - dayOfWeek;
var deltaMS = deltaDays * constants_1.msPerDay;
var adjustedTime = theDate.getTime() + deltaMS;
var adjustedDate = new Date(adjustedTime);
return adjustedDate;
}
}
exports.getOffSetDayOfWeek = getOffSetDayOfWeek;
//# sourceMappingURL=weeks.js.map