Skip to content

Instantly share code, notes, and snippets.

@ikouchiha47
Created January 11, 2016 05:16
Show Gist options
  • Save ikouchiha47/f24856e0905c44ce3e64 to your computer and use it in GitHub Desktop.
Save ikouchiha47/f24856e0905c44ce3e64 to your computer and use it in GitHub Desktop.
const fullDates = ["Monday", "Tuesday", "Wednesday", "Thrusday", "Friday", "Saturday", "Sunday"];
const shortDates = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
const fullMonths = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const shortMonths = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
const re = /(MM?M?M?|ddd?d?|YYYY|YY|HH?|hh?|mm?|tt|.)/g;
function contains(array, props) {
if(!props.splice) props = [props];
if(props.length < 2) return array.indexOf(props[0]) > -1;
for(let i = 0, len = props.length; i < len; i++) {
if(array.indexOf(props[i]) < 0) return false;
}
return true;
};
function DateFactory(date = {}, seperator = ".") {
if(date instanceof Date) {
this._date = { year: date.getFullYear(), month: date.getMonth(), date: date.getDate() };
this._dateObj = date;
} else {
this._date = {...date};
}
// because current database has day in fromDate which should be date
if(!this._date.day) {
this._date.day = this._date.date;
}
if(!this._date.date) {
this._date.date = this._date.day;
}
if(contains(Object.getOwnPropertyNames(date), ["month", "date", "year"])) {
this._dateObj = new Date(date.year, date.month, date.date);
}
}
DateFactory.prototype.isLeapYear = function(date = {}) {
return new Date(date.year, 1, 29).getMonth() === 1;
};
DateFactory.prototype.isPresent = function() {
return Object.getOwnPropertyNames(this._date).contains("month", "year");
};
DateFactory.prototype.formatDate = function(formatTemplate = "dd.MM.YYYY") {
const tokens = formatTemplate.match(re);
return tokens.map(token => {
switch(token) {
case 'd':
return this._date.day ? this._date.day : "";
case 'dd':
return (this._date.day / 10 < 0) ? "0" + this._date.day : (this._date.day == 0 ? "" : this._date.day);
case 'ddd':
return shortDates[this._dateObj.getDay()]
case 'dddd':
return fullDates[this._dateObj.getDay()]
case 'M':
return this._date.month ? this._date.month : "";
case 'MM':
return (this._date.month / 10 < 0) ? "0" + this._date.month : (this._date.month == 0 ? "" : this._date.month);
case 'MMM':
return shortMonths[this._date.month];
case 'MMMM':
return fullMonths[this._date.month]
case 'YY':
return (new Date("" + this._date.year)).getYear();
case 'YYYY':
return (new Date("" + this._date.year)).getFullYear();
case 'HH':
if(this._dateObj) {
let hours = this._dateObj.getHours();
return (hours / 10 < 0) ? "0" + hours : hours;
}
case 'hh':
if(this._dateObj) {
let hours = this._dateObj.getHours();
hours = hours > 12 ? hours - 12 : hours;
return (hours / 10 < 1) ? "0" + hours : hours;
}
case 'mm':
if(this._dateObj) {
let minutes = this._dateObj.getMinutes();
return (minutes / 10 < 1) ? "0" + minutes : minutes;
}
case 'tt':
if(this._dateObj) {
return this._dateObj.getHours() > 12 ? "PM" : "AM";
}
default:
return token;
}
}).join("");
};
DateFactory.compareDates = function(fromDateObj = {}, toDateObj = {}) {
var fromDate = fromDateObj, toDate = toDateObj;
if(fromDateObj instanceof Date) {
fromDate = { year: fromDateObj.getFullYear(), month: fromDateObj.getMonth() + 1, day: fromDateObj.getDate() };
}
if(toDateObj instanceof Date) {
toDate = { year: toDateObj.getFullYear(), month: toDateObj.getMonth() + 1, day: toDateObj.getDate() };
}
if(fromDate.year < toDate.year) return 1;
if(fromDate.year > toDate.year) return -1;
if(fromDate.month < toDate.month) return 1;
if(fromDate.month > toDate.month) return -1;
if(fromDate.day < toDate.day) return 1;
if(fromDate.day > toDate.day) return -1;
return 0;
};
export default DateFactory;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment