Calendar Weeks? (ISO week number)
Hi,
is there a way to work with calendar weeks instead of a single day?
When somebody sets a date/time (date only) field, I like to work with that date as calendar week. The reason behind is that the solution I build is about a weekly report. It can be the case that a user is not working all the days at the calendar week and therefore might not submit his report at the end of the week. So I end up with various dates that belong to a calendar week of course. No matter when the report gets created or when it will be submitted, it will always be for a specific calendar week. If a user enters April 17 I want to know what Calendar Week that is and use the Calendar Week to go further. At the end the visual display, however, should be something like "CW 16, APR 15-19, 2024"
Example:
If a user enters April 19, same as above, it should result into something like this "CW 16, APR 15-19, 2024"
If a user enters April 26, it should be CW 17 etc. like "CW 17, APR 22-26, 2024"
Alternatively of course, if there is a way to let the user choose a week instead of a single day, that would also satisfy my needs. The reason behind is, that when having various lists, I can easily create reporting views, based on calendar weeks.
Open for any ideas and discussion :)
Many thanks in advance!
Cheers
Joerg
-
Okay, I am not a programmer, however, I managed to steal code and copy and paste it together to get a working result, maybe the code can be optimized?
// load the moment.js library
var url = "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js";
var fileName = "momentjs";
var moduleCache = window.module;
var defineCache = window.define;
window.module = undefined;
window.define = undefined;
window.SP.SOD.registerSod(fileName, url);
window.LoadSodByKey(fileName, null, true);
window.module = moduleCache;
window.define = defineCache;
//parse date by moment.js and get an integer representation
var dateInt = Date.parse(moment([[ReportDate]]));
//create a date object out of it and get the week number
var d = new Date(dateInt);
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
return Math.ceil((((d - yearStart) / 86400000) + 1) / 7)0 -
Finally what I wanted is to have a reporting period like 2024.01 (for the first week of the year) up to 2024.52 (respectively 53 for the last week of the year), in order to make my reports sortable my end result in code is like this:
// load the moment.js library
var url = "https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.js";
var fileName = "momentjs";
var moduleCache = window.module;
var defineCache = window.define;
window.module = undefined;
window.define = undefined;
window.SP.SOD.registerSod(fileName, url);
window.LoadSodByKey(fileName, null, true);
window.module = moduleCache;
window.define = defineCache;
//parse date by moment.js and get an integer representation
var dateInt = Date.parse(moment([[ReportDate]]));
//create a date object out of it and get the week number
var d = new Date(dateInt);
var dayNum = d.getUTCDay() || 7;
d.setUTCDate(d.getUTCDate() + 4 - dayNum);
var yearStart = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
//return year.ISOweek
return d.getFullYear() + "." + String(Math.ceil((((d - yearStart) / 86400000) + 1) / 7)).padStart(2, '0');0
Please sign in to leave a comment.
Comments
2 comments