ISO week number

Questions, tips and tricks and techniques for scripting in Servoy

ISO week number

Postby Dexadrine » Fri Sep 15, 2006 3:45 pm

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.
Dexadrine
 
Posts: 96
Joined: Wed Jun 07, 2006 12:25 pm
Location: London

Postby patrick » Fri Sep 15, 2006 4:26 pm

In a few days I will release a free plugin (DateUtils) that provides all kinds of convenient date functions. Getting the week is one of them...

In the meantime you could do:

Code: Select all
var vCalendar = java.util.Calendar.getInstance();
var vWeek = vCalendar.get(java.util.Calendar.WEEK_OF_YEAR);
Patrick Ruhsert
Servoy DACH
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Re: ISO week number

Postby itgenetics » Mon Jul 25, 2011 5:28 pm

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?
itgenetics
 
Posts: 63
Joined: Fri Apr 08, 2011 11:26 am
Location: Cambridge, UK

Re: ISO week number

Postby ROCLASI » Mon Jul 25, 2011 5:42 pm

Hi,

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.

Hope this helps.
Robert Ivens
SAN Developer / Servoy Valued Professional / Servoy Certified Developer

ROCLASI Software Solutions / JBS Group, Partner
Mastodon: @roclasi
--
ServoyForge - Building Open Source Software.
PostgreSQL - The world's most advanced open source database.
User avatar
ROCLASI
Servoy Expert
 
Posts: 5438
Joined: Thu Oct 02, 2003 9:49 am
Location: Netherlands/Belgium

Re: ISO week number

Postby patrick » Tue Jul 26, 2011 9:07 am

With the plugin it is also easy:

Code: Select all
var vIsoWeek = plugins.DateUtils.DateTime().weekOfYear;
Patrick Ruhsert
Servoy DACH
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Re: ISO week number

Postby omar » Tue Jul 26, 2011 10:17 am

The following code supports both ISO and canadian week numbering:

Code: Select all
// 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;   
}


Regards,
Omar
Intrasoft, Founder
Omar van Galen
omar@intrasoft.nl
+31-(0)6-21234586
Servoy Developer
omar
 
Posts: 377
Joined: Sat Feb 12, 2011 4:51 pm
Location: Intrasoft, The Netherlands

Re: ISO week number

Postby jcompagner » Tue Jul 26, 2011 10:26 am

patrick wrote:With the plugin it is also easy:

Code: Select all
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.
Johan Compagner
Servoy
User avatar
jcompagner
 
Posts: 8829
Joined: Tue May 27, 2003 7:26 pm
Location: The Internet

Re: ISO week number

Postby patrick » Tue Jul 26, 2011 10:29 am

The plugin has a property

Code: Select all
plugins.DateUtils.minimalDaysInFirstWeek


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.
Patrick Ruhsert
Servoy DACH
patrick
 
Posts: 3703
Joined: Wed Jun 11, 2003 10:33 am
Location: Munich, Germany

Re: ISO week number

Postby itgenetics » Tue Jul 26, 2011 10:50 am

Thank you all very much for your input. I have found this very useful.
itgenetics
 
Posts: 63
Joined: Fri Apr 08, 2011 11:26 am
Location: Cambridge, UK


Return to Methods

Who is online

Users browsing this forum: No registered users and 10 guests