How to get week number in variable

Dec 01, 2014

Hi, 

I'm building a weekly quiz in Storyline.  It's going to be an html-only quiz with 52 questions and games, displaying one according to which week of the year it is. 

I've been trying some code I found online to get the week number through javascript and put in in a variable. But I haven't got it to work. 

Does anybody know which code will work?

Thanks!

Ayoub

6 Replies
Michael Hinze

I did a quick test using this code and a Storyline variable "MyWeekNumber", that worked for me.

"

Date.prototype.getWeek = function() {
 var onejan = new Date(this.getFullYear(),0,1);
 return Math.ceil((((this - onejan) / 86400000) + onejan.getDay()+1)/7);
 }

var today = new Date();
 var weekno = today.getWeek();

var player = GetPlayer();
 player.SetVar("MyWeekNumber",weekno);

"

Derek Latimer

HI, i too am looking for a piece of java script to pull the 'week of year' in from a calendar, i tried the code above but it didnt seem to work, (it said week 10 instead of week 9) for 29th feb, possibly as 1st jan this year is week 53 of 2015, Is anyone aware of some code that takes this into account?

 

many thanks

Derek

Christie Pollick

Hi, Derek -- Thanks for reaching out here! I just thought I should stop in to note that unfortunately, we are not able to provide support for use of JavaScript. Luckily, we have lots of community members who are usually more than willing to share their expertise to assist!

And in case you'd like to check it out, here is a sheet on JavaScript Best Practices you may want to keep handy. :)

Job Vlaming

Hi Derek, 

I use this code to get the week number:

var player = GetPlayer();
var weekNumber = Math.ceil(((((new Date()-new Date(new Date().getFullYear(),0,1))/8.64e7)+4)/7)-1);
player.SetVar("rWeekNumber", weekNumber);

rWeekNumber is the Storyline variable I use, change it to whatever you use.

Change the +4  to tweak whatever day you use as first day of the week. 

Use the -1 to correct the week number as needed: some years start with week 53. 

You'll need to update/correct this code every year. I think this is set right for 2016, with weeks starting on Monday. The week number may be off by 1. For my purpuses it didn't matter, all I wanted to do is show a certain page according to the returned value, I never display the variable.

Bonus tip to test your tweaking: output to html, and change your computer's system date back and forth to see if the correct week number is calculated (don't forget to change it back, I had people wondering how I was emailing them from the future).

Good luck! 

Cheers, Ayoub

This discussion is closed. You can start a new discussion or contact Articulate Support.