Friday 9 December 2011

Some useful classes

Cookie class

function Coockie(){
 this.create = function(name,value,days) {
  days = days || 999999;
  if (days) {
   var date = new Date();
   date.setTime(date.getTime()+(days*24*60*60*1000));
   var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  this.coockie_cache[name] = value;
  document.cookie = name+"="+value+expires+"; path=/";
 }

 this.read = function(name) {
  if(this.coockie_cache[name]) return this.coockie_cache[name];
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
   var c = ca[i];
   while (c.charAt(0)==' ') c = c.substring(1,c.length);
   if (c.indexOf(nameEQ) == 0) return this.coockie_cache[name] = c.substring(nameEQ.length,c.length);
  }
  return null;
 }

 this.eraseCookie = function(name) {
  createCookie(name,"",-1);
 }

 this.coockie_cache = {};
}
fireBug debug times class

var Debug = function(o){
 if('time' in console){
  if(Debug.times.length>0) console.timeEnd(Debug.times[Debug.times.length-1].name);
  Debug.times.push({name:o});
  console.time(o);
 }
}
Debug.times = [];
Debug.stop=function(){
 if('time' in console){
  for(var i=0;i< Debug.times.length-1;i++){
   Debug.times[i].total = console.timeEnd(Debug.times[i].name);
  }
  for(var i=0;i< Debug.times.length-1;i++){
   //log(Debug.times[i].name+": "+(Debug.times[i].total-Debug.times[i+1].total))
   //Debug.times[i].total = console.timeEnd(Debug.times[i].name);
  }
 }
};
js date week functions for date object

Date.prototype.getWeek = function () {  
    // Create a copy of this date object  
    var target  = new Date(this.valueOf());  
  
    // ISO week date weeks start on monday  
    // so correct the day number  
    var dayNr   = (this.getDay() + 6) % 7;  
  
    // ISO 8601 states that week 1 is the week  
    // with the first thursday of that year.  
    // Set the target date to the thursday in the target week  
    target.setDate(target.getDate() - dayNr + 3);  
  
    // Store the millisecond value of the target date  
    var firstThursday = target.valueOf();  
  
    // Set the target to the first thursday of the year  
    // First set the target to january first  
    target.setMonth(0, 1);  
    // Not a thursday? Correct the date to the next thursday  
    if (target.getDay() != 4) {  
        target.setMonth(0, 1 + ((4 - target.getDay()) + 7) % 7);  
    }  
  
    // The weeknumber is the number of weeks between the   
    // first thursday of the year and the thursday in the target week  
    return 1 + Math.ceil((firstThursday - target) / 604800000); // 604800000 = 7 * 24 * 3600 * 1000  
} 

Date.prototype.getWeekYear = function ()   
{  
    // Create a new date object for the thursday of this week  
    var target  = new Date(this.valueOf());  
    target.setDate(target.getDate() - ((this.getDay() + 6) % 7) + 3);  
    return target.getFullYear();  
}

Date.prototype.addDays = function(nr_of_days){
 var a = new Date(this.valueOf());;
 return nr_of_days==0?this:new Date(a.setDate(a.getDate()+nr_of_days));
}
Date.prototype.addDaysToSunday = function(){
 return this.addDays(
  7 - (this.getDay()==0?7:this.getDay())
 );
}
Date.prototype.moveToStartOfNextWeek = function(){
 return this.addDays(
  8 - (this.getDay()==0?7:this.getDay())
 );
}
Date.prototype.addWeeks = function(weeks){
 if(weeks<1) return this;
 return this.addDays(
  8 - (this.getDay()==0?7:this.getDay()) + 7*(weeks-1)
 );
}
Date.prototype.getISODay = function(){
 // Native JS method - Sunday is 0, monday is 1 etc.
 var d = this.getDay();
 // Return d if not sunday; otherwise return 7
 return d ? d : 7;
};

Date.prototype.getMaxWeekOfYear = function (){
 var year = this.getFullYear();
 var maxWeek = 52;
 fj = new Date(year, 0, 1); //1st Jan
 tfd = new Date(year, 11, 31); //31st Dec
 if(fj.getDay() == 4 || tfd.getDay() == 4){
  maxWeek = 53;
 }
 return maxWeek;
}

No comments:

Post a Comment