Does anyone know of a method or calc to work out the ISO week number from a time stamp field?
ISO 8601 week numbers determines week 1 of the year as being the one with the 1st Thursday. So for example 1st January 2006 was using the ISO standard week 52 of 2005.
The months are determined by the week periods. 4,4,5. i.e. January has weeks 1-4, February has weeks 5-8, March has weeks 9-13, April has weeks 14-17, etc.
Hi Patrick - I too need a solution to this. I see that you have a plugin (which I have downloaded). For the industry I am currently working in, Week 1 starts on the first week whereby 1st January is on or before the Thursday of that week. Can you give me an example of how to use your plugging to achieve that please?
If I am not mistaken that is the ISO week numbering, right ?
You might want to take a look at the OpenSource mod_datejs module from Greg Pierce. This module is an implementation of the OpenSource JavaScript Date library datejs.
For information on the API see http://code.google.com/p/datejs/wiki/APIDocumentation .
Search for getWeek and getISOWeek.
The following code supports both ISO and canadian week numbering:
// VFP2Servoy Toolkit
// Function : WEEK()
// Author : Omar van Galen
/**
* Returns the week number for the date parameter dDate.
* It supports both ISO 8601 and USA/Canada week numbering.
* This function can be written shorter but I prefer to
* keep it readable and understandable
*
* @param {Date} dDate
* @param {Number} nOverruleFirstDayOfWeek (optional) - 0=Sunday 1=Monday
* @return {Number} number
*/
function WEEK(dDate, nOverruleFirstDayOfWeek) {
// Remove time components of date
var justTheDate=new Date(dDate.getFullYear(), dDate.getMonth(), dDate.getDate());
var weekDiff=0;
// determine if ISO 8601 (european) or Canada/USA calculation should be used
if((!nOverruleFirstDayOfWeek && utils.isMondayFirstDayOfWeek()) ||
nOverruleFirstDayOfWeek==1){
// ISO 8601 (week 1 contains Jan 4th and first Thursday is in week 1)
// Change date to Thursday same week
var targetThursday = new Date(justTheDate.setDate(justTheDate.getDate() -
((justTheDate.getDay() + 6) % 7) + 3));
// Take January 4th as it is always in week 1 (see ISO 8601)
var targetJan4 = new Date(targetThursday.getFullYear(), 0, 4);
// Change date to Thursday same week
var firstThursday = new Date(targetJan4.setDate(targetJan4.getDate() -
((targetJan4.getDay() + 6) % 7) + 3));
// Number of weeks between target Thursday and first Thursday
weekDiff = (targetThursday - firstThursday) / (86400000*7);
}else{
// Canada/USA (week 1 contains Jan 1st and first Saturday is in week 1)
// Change date to Saturday same week
var targetSaturday = new Date(justTheDate.setDate(justTheDate.getDate() -
((justTheDate.getDay() + 6) % 7) + 5));
// Take January 1st as it is always in week 1
var targetJan1 = new Date(targetSaturday.getFullYear(), 0, 1);
// Change date to Saturday same week
var firstSaturday = new Date(targetJan1.setDate(targetJan1.getDate() -
((targetJan1.getDay() + 6) % 7) + 5));
// Number of weeks between target Saturday and first Saturday
weekDiff = (targetSaturday - firstSaturday) / (86400000*7);
}
// return the result
return 1 + weekDiff;
}
var vIsoWeek = plugins.DateUtils.DateTime().weekOfYear;
patrick,
i guess you have to have an extra argument for DateTime() like DateTime(locale)
because it you really want to know what the week number is in the USA and you are in europa then you have to ask it for that specific locale…
Because our week numbers differ… For us the first week is where the first thursday is in, for them it is where the 1st of January is in.
that takes a number of days and sets what the minimal days required in the first week of the year are; For example, if the first week is defined as one that contains the first day of the first month of a year, call the method with value 1. If it must be a full week, use value 7. The property defaults to the client’s default, but can be overridden.