Skip to main content

Search

Calendar Weeks? (ISO week number)

Comments

2 comments

  • Joerg_K.

    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
  • Joerg_K.

    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.