Created
August 29, 2014 18:09
-
-
Save mateusfreira/8f9abbae85a31fb42dad to your computer and use it in GitHub Desktop.
dateUtil
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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