js to compute week number ?

13 views
Skip to first unread message

Jacques Turbé

unread,
Apr 6, 2006, 6:30:18 AM4/6/06
to TiddlyWiki
I use heavily weeknumbers as tags for weeklyreviews, etc.

For instance this week is : "6w14".

Now I enter them each time manually, but I'd like a script or macro in
which you input Y M D, and which outputs the computed week : "14", or
better "6w14" (for today)

Here is the shorter algo I found :


function getWeekNumber(month, day, year)
{
var now = new Date(year, month, day + 1);
var then = new Date(year, 0, 1);
var firstDay = new Date(year, 0, 1);
var compensation = firstDay.getDay() + 3;
return Math.round((((now - then) / 86400000) + compensation) / 7);
}


Thanks for help :)

Jacques

Simon Baird

unread,
Apr 6, 2006, 10:53:10 PM4/6/06
to Tiddl...@googlegroups.com
If you take this:
and your function below, I think you are 99% done..
The other 1% might be parseInt:
 
 
Simon.

 
--
Simon Baird <simon...@gmail.com>

Jacques Turbé

unread,
Apr 7, 2006, 7:18:11 AM4/7/06
to TiddlyWiki
Thanks Simon,

I still miss something, as my result is : "6sNaN"

Here was my try :

config.macros.weekNb = {};
config.macros.weekNb.handler = function
(place,macroName,params,wikifier,paramString,tiddler) {
wikify("6s" + getWeekNumber(), place);


}
function getWeekNumber(month, day, year)
{
var now = new Date(year, month, day + 1);
var then = new Date(year, 0, 1);
var firstDay = new Date(year, 0, 1);
var compensation = firstDay.getDay() + 3;
return Math.round((((now - then) / 86400000) + compensation) / 7);
}


getDay() has to be defined or has another name in TW or Dateplugin ?


Question :
Is there a thread, or a tutorial where macro parameters are explained :
I copy without really understanding this line :

function (place,macroName,params,wikifier,paramString,tiddler) { ...


Thanks,

Jacques

Jeremy Ruston

unread,
Apr 7, 2006, 7:24:43 AM4/7/06
to Tiddl...@googlegroups.com
I'd like to see this week number thing go into the core -- you could
easily imagine being able to put 'WW' and '0WW' in date format
templates.

But, it appears that there's several competing week numbering systems:

http://www.rondebruin.nl/weeknumber.htm

I'd be interested in a concise, flexible implementation of
Date.prototype.getWeek() that could cope with all these various
schemes.

Cheers

Jeremy.

--
Jeremy Ruston
mailto:jer...@osmosoft.com
http://www.tiddlywiki.com

Russ Thomas

unread,
Apr 7, 2006, 8:36:11 AM4/7/06
to Tiddl...@googlegroups.com
Week 53 indeed.

I recall grappling with this mess around 1991 or so. When I saw this
post I figured there wasn't much point in responding since by now,
surely, "the world" would have got its act together and fixed it.

Silly me.

I'd propose that core TW implement the ISO8601 and stop there.
core.getWeekNo (or whatever) can be overidden/hijacked on a case by
case (TW by TW) basis.

I can't imagine a situation where one TW would need more than one
standard for week numbers (except, perhaps, in that rare circumstance
where its purpose was to educate us as to the weirdness of dates in
general and the need to convert between them).

In fact, it's my opinion that core systems - I include TW here -
should return ISODATE8601
(http://www.iso.org/iso/en/prods-services/popstds/datesandtime.html)
and let "flavour" plugins deal with the rest.

just my 0.02

russ

Jeremy Ruston

unread,
Apr 7, 2006, 9:56:12 AM4/7/06
to Tiddl...@googlegroups.com
I've spent a little time looking at this. It turns out to be a bit
more complicated. In particular, there are situations where the first
few days of January or the last few days of December are placed in the
previous or following year. This means that when you use the 'WW'
format to get a week number that the behaviour of an accompanying
'YYYY' format should change to reflect the adjusted year. I can't see
a good (ie non-confusing) way of doing that, so I propose to leave
this feature to specialist plugins. Sorry!

Cheers

Jeremy.

Simon Baird

unread,
Apr 7, 2006, 5:51:34 PM4/7/06
to Tiddl...@googlegroups.com
On 07/04/06, Jacques Turbé <jacque...@gmail.com> wrote:

Thanks Simon,

I still miss something, as my result is : "6sNaN"

Here was my try :

config.macros.weekNb = {};
config.macros.weekNb.handler = function
(place,macroName,params,wikifier,paramString,tiddler) {
wikify("6s" + getWeekNumber(), place);
}


You have to have some function arguments

Test like this:
getWeekNumber(2,3,2006);

Then this to pass in the macro params:
getWeekNumber(params[0].parseInt(),params[1].parseInt(),params[2].parseInt());
 
Reply all
Reply to author
Forward
0 new messages