Skip to content

Instantly share code, notes, and snippets.

@mateusfreira
Created August 29, 2014 18:09
Show Gist options
  • Save mateusfreira/8f9abbae85a31fb42dad to your computer and use it in GitHub Desktop.
Save mateusfreira/8f9abbae85a31fb42dad to your computer and use it in GitHub Desktop.
dateUtil
//Date Util
Date.prototype.getYearGap = function(){
var ret = new Date(this.getTime());
return this.getFullYear() - ret.getFullYear();
};
Date.prototype.getStartOfMonth = function(){
var ret = new Date(this.getTime());
ret.setDate(1)
return ret;
};
Date.prototype.getEndOfMonth = function(){
var lastDayOfMonth = new Date(this.getFullYear(), this.getMonth()+1, 0);
return lastDayOfMonth;
}
Date.prototype.getSixMonthAgo= function(){
var ret = new Date(this.getTime());
ret.setMonth(ret.getMonth()-6)
return ret;
};
Date.prototype.getAYearAgo = function(){
var ret = new Date(this.getTime());
ret.setFullYear(ret.getFullYear()-1)
return ret;
};
Date.prototype.getStartOfWeek= function(){
var d = new Date(this.getTime());
d.setDate(d.getDate()-d.getDay())
return d;
};
Date.prototype.getDateBr = function(){
return window.getDateBr(this);
};
Date.prototype.getHourBr = function(){
return window.getHourBr(this);
};
Date.prototype.getHourBr = function(){
return window.getHourBr(this);
};
Date.prototype.getNextMonth = function(){
var ret = new Date(this.getTime());
ret.setMonth(ret.getMonth()+1);
return ret;
}
Date.prototype.getMonthBrName = function(){
return i18n_months[this.getMonth()];
};
Date.prototype.getServerTime = function(){
var promise = $.get("/system/date");
var startRequest = new Date().getTime();
promise.success(function(value){
var current = (new Date().getTime());
diff = current - value;
real_diff = diff - (current - startRequest);
localStorage.setItem('diff_time', real_diff);
});
};
var FactoryDate = {};
FactoryDate.byTime = function(time){
return new Date(time);
};
Date.prototype.getDateUs = function(){
var d = new Date(this.getTime());
month = d.getMonth()+1;
if(month <10 ){
month = "0"+month;
}
var day = d.getDate();
if(day <10){
day = "0"+day;
}
var date_str = d.getFullYear()+"-"+month+"-"+day;
return date_str;
};
Date.prototype.getTextWhen = function(){
return i18n_days[this.getDay()]+" "+this.getDate()+" de "+i18n_months[this.getMonth()]+" de "+this.getFullYear();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment