Skip to main content
Known Participant
February 5, 2018
Question

How do i change this?

  • February 5, 2018
  • 11 replies
  • 939 views

/*** Get the ISO week date week number */

Date.prototype.getWeek = function () {
    // Create a copy of this date object;
    var target  = new Date(this.valueOf());
    //var target  = new Date(this.getField("WeekBegin").value);

    // ISO week date weeks start on Monday;
    // so correct the day number;
    var dayNr   = (this.getDay() + 6) % 7;

    // ISO 8601 states that week 1 is the week;
    // with the first Thursday of that year.;
    // Set the target date to the Thursday in the target week;
    target.setDate(target.getDate() - dayNr + 3);

    // Store the millisecond value of the target date;
    var firstThursday = target.valueOf();

    // Set the target to the first Thursday of the year;
    // First set the target to January first
    target.setMonth(0, 1);
    // Not a Thursday? Correct the date to the next Thursday
    if (target.getDay() != 4) {
        target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);
    }

    // The weeknumber is the number of weeks between the
    // first Thursday of the year and the Thursday in the target week
    return 1 + Math.ceil((firstThursday - target) / 604800000); // 604800000 = 7 * 24 * 3600 * 1000
}

var oDate = new Date(); // create date object;
var nWeekNumber = oDate.getWeek(); // get the week number for the date object;
//app.alert("For " + util.printd("ddmmyy", oDate) + " it is week " + nWeekNumber, 3, 0);

this.getField("WeekNumber").value = nWeekNumber;

Now I Want to be able to use the "weekbegin" field text box (ddmmyy) to work out the week number, but instead its using the current date.

What do I change to get it to use the "weekbegin" as the date instead of the current date it uses?

This topic has been closed for replies.

11 replies

try67
Community Expert
Community Expert
February 5, 2018

Change this line:

var oDate = new Date();

To:

var oDate = util.scand("mm/dd/yyyy", this.getField("weekbegin").valueAsString);

Adjust the date format if necessary.

However, this will return the results for the current date if the field is empty, so you might want to add a condition that checks for that.

WirepowerAuthor
Known Participant
February 5, 2018

Ok I tried that, but doesn't show any results.

var oDate = util.scand("mmddyy", this.getField("WeekBegin").valueastring);

is what I have, However if the "WeekBegin" field is filled or not it doesn't show any value?

try67
Community Expert
Community Expert
February 5, 2018

That's not the code I posted.