Skip to content

Instantly share code, notes, and snippets.

@oleq
Created November 21, 2012 17:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oleq/4126415 to your computer and use it in GitHub Desktop.
Save oleq/4126415 to your computer and use it in GitHub Desktop.
var getWeek = function(d) {var e=new Date(d),f=e.setDate(d.getDate()-(d.getDay()+6)%7+3);e.setMonth(0,4);return((f-e)/6048e5)+1.5|0};
var testData = {
'2005-01-01': 53,
'2005-01-02': 53,
'2005-12-31': 52,
'2007-01-01': 01,
'2007-12-30': 52,
'2007-12-31': 01,
'2008-01-01': 01,
'2008-12-29': 01,
'2008-12-31': 01,
'2009-01-01': 01,
'2009-12-31': 53,
'2010-01-03': 53,
'2009-12-31': 53,
'2010-01-01': 53,
'2010-01-02': 53,
'2010-01-03': 53,
'2008-12-28': 52,
'2008-12-29': 01,
'2008-12-30': 01,
'2008-12-31': 01,
'2009-01-01': 01,
'1970-12-30': 53,
'2008-09-26': 39
};
for ( var i in testData ) {
var d = new Date( i ),
stamp = +d,
week;
if ( ( week = getWeek( d ) ) == testData[ i ] ) {
if ( +d == stamp )
console.log( 'OK', i, testData[ i ] )
else
console.log( 'OK but \'d\' has changed. Was: ' + new Date( i ) + ', is: ' + d );
}
else
console.log( 'Wrong: week for ' + i + '. Is: ' + week + ', Should be: ' + testData[ i ] );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment