mercoledì 22 ottobre 2014

Javascript Date

ENG: This are some usefull javascript date functions. I know that there are so many and best off that, but are the ones I often use in my job, so I decided to share and put them in convenient place.
In javascript  date variables are objects, assignment are by reference not by value, so you can't simply duplicate variable assigning it's value to another. The cloneDate function duplicates variables of date type.

ITA: Ho voluto raccogliere alcune utili funzioni javascript per la manipolazione delle date. Lo sò che ne esistono tante e migliori di queste, ma sono quelle che uso spesso per il mio lavoro, perciò ho deciso di condividerle e di metterle in posto comodo.
In javascript le date sono degli oggetti, l'assegnazione è per riferimento e non per valore, quindi non è possibile duplicare una variabile data assegnando il valore ad un'atra variabile.
La funzione cloneDate serve appunto per duplicare una variabile di tipo data.


cloneDate = function(d) {
// Duplicate Date Type Variable
  var dd = new Date();
  dd.setTime(d.getTime());
  return(dd);
}
yesterday = function() {
  var dd = new Date();
  dd.setDate(dd.getDate()-1);
  return(dd);
}
tomorrow = function() {
  var dd = new Date();
  dd.setDate(dd.getDate()+1);
  return(dd);
}
roundDo = function(d) {
// Arrotonda alla Domenica precedente
// Round date to the previous Sunday
  var dd = cloneDate(d);
  dd.setDate(dd.getDate() - dd.getDay());
  return(dd);
};
roundLu = function(d) {
// Arrotondo al Lunedì precedente
// Round to the previous Monday
  var dd = cloneDate(d);
  dd.setDate(dd.getDate() - 1);
  dd.setDate(dd.getDate() - dd.getDay() +1);
  return(dd);
};
roundMe = function(d) {
// Arrotondo al Mercoledì precedente
// Round to the previus Wednesday
  var dd = cloneDate(d);
  dd.setDate(dd.getDate() + 4);
  dd.setDate(dd.getDate() - dd.getDay() -4);
  return(dd);
};
roundGi = function(d) {
// Arrotondo al giovendì precedente
// Round to the previuos Thursday
  var dd = cloneDate(d);
  dd.setDate(dd.getDate() + 3);
  dd.setDate(dd.getDate() - dd.getDay() -3);
  return(dd); 
};
roundFirst = function(d) {
// Arrotonda la primo giorno del mese
// Round to the first day of month
  var dd = cloneDate(d);
  dd.setDate(1);
  return(dd); 
};
roundLast = function(d) {
// Arrotonda all'ultimo giorno del mese
// Round to the last day of month
  var dd = cloneDate(d);
  dd.setDate(1);
  dd.setMonth(d.getMonth()+1);
  dd.setDate(0);
  var fmtdd=formatDT(dd);
  var fmtM2=formatDT(dateM2);
  if (date.compare(dd, dateM2) > 0){
    dd=dateM2;
  }
  return(dd); 
};
roundLastCompleteMonth = function() {
// Primo giorno dell'ultimo mese completo 
// First day of the last completed month 
  var d = new Date();
  return(new Date(d.getFullYear(), d.getMonth() - 1, 1));
};