Skip to main content
This topic has been closed for replies.

3 replies

Participant
April 24, 2016

thank to both the answers - it is now working

Inspiring
April 24, 2016

The following document level scripts will return the week number of the year for a specific date and the 4 digit year for that date.

Document level scripts to add the getWeek method and getWeekYear method for the date object;

// This script is released to the public domain and may be used, modified and
// distributed without restrictions. Attribution not necessary but appreciated.
// Source: http://weeknumber.net/how-to/javascript

// Returns the ISO week of the date.
Date.prototype.getWeek = function() {
  var date = new Date(this.getTime());
   date.setHours(0, 0, 0, 0);
  // Thursday in current week decides the year.
  date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
  // January 4 is always in week 1.
  var week1 = new Date(date.getFullYear(), 0, 4);
  // Adjust to Thursday in week 1 and count number of weeks from date to week1.
  return 1 + Math.round(((date.getTime() - week1.getTime()) / 86400000
                        - 3 + (week1.getDay() + 6) % 7) / 7);
}

// Returns the four-digit year corresponding to the ISO week of the date.
Date.prototype.getWeekYear = function() {
  var date = new Date(this.getTime());
  date.setDate(date.getDate() + 3 - (date.getDay() + 6) % 7);
  return date.getFullYear();
}

Assuming your form has a field named "MyDate" for the date you need the week number and week number year for with a date format of "mm/dd/yyyy" the following custom JavaScript calculation will compute the week number and week number year and the number of day of the week  for the result field:

var cFieldName = "MyDate"; // date field name;

var cDateFormat = "mm/dd/yyyy"; // date format for the date fields;

var cDateField = this.getField(cFieldName).value; // get date field value;

var oDate = util.scand(cDateFormat, cDateField); // convert date field value to date object;

var nDay = oDate.getDay(); // get number of day of the week;

if(nDay == 0) nDay = 7;

event.value =  oDate.getWeekYear() + "-W" + util.printf("%,102.0f", oDate.getWeek()) + "-" + util.printf("%,102.0f", nDay); // combine the week number and weekyear;

etWeekYear(); // combine the week number week year and day of the week;

You can adjust the field name and date format as needed.

try67
Community Expert
Community Expert
April 24, 2016

There are several tutorials online that explain how to achieve it. I adjusted one of the codes and you can use it like this (let's say you have a field called "WeekNumber"), as a doc-level script in your file:

this.getField("WeekNumber").value = getWeekNumber(new Date());

function getWeekNumber(d) {

    // Copy date so don't modify original

    d = new Date(+d);

    d.setHours(0,0,0);

    // Set to nearest Thursday: current date + 4 - current day number

    // Make Sunday's day number 7

    d.setDate(d.getDate() + 4 - (d.getDay()||7));

    // Get first day of year

    var yearStart = new Date(d.getFullYear(),0,1);

    // Calculate full weeks to nearest Thursday

    var weekNo = Math.ceil(( ( (d - yearStart) / 86400000) + 1)/7);

    // Return array of year and week number

    return weekNo;

}

I didn't test the code very thoroughly, though, so it might have some bugs in it, but you can easily replace it with something else, if you wish. Just google "javascript get week number" to find more examples.