UNPKG

@ysmood/material-ui

Version:

Material Design UI components built with React

168 lines (139 loc) 3.95 kB
module.exports = { addDays: function(d, days) { var newDate = this.clone(d); newDate.setDate(d.getDate() + days); return newDate; }, addMonths: function(d, months) { var newDate = this.clone(d); newDate.setMonth(d.getMonth() + months); return newDate; }, addYears: function(d, years) { var newDate = this.clone(d); newDate.setFullYear(d.getFullYear() + years); return newDate; }, clone: function(d) { return new Date(d.getTime()); }, cloneAsDate: function(d) { var clonedDate = this.clone(d); clonedDate.setHours(0,0,0,0); return clonedDate; }, getDaysInMonth: function(d) { var resultDate = this.getFirstDayOfMonth(d); resultDate.setMonth(resultDate.getMonth() + 1); resultDate.setDate(resultDate.getDate() - 1); return resultDate.getDate(); }, getFirstDayOfMonth: function(d) { return new Date(d.getFullYear(), d.getMonth(), 1); }, getFullMonth: function(d) { var month = d.getMonth(); switch (month) { case 0: return 'January'; case 1: return 'February'; case 2: return 'March'; case 3: return 'April'; case 4: return 'May'; case 5: return 'June'; case 6: return 'July'; case 7: return 'August'; case 8: return 'September'; case 9: return 'October'; case 10: return 'November'; case 11: return 'December'; } }, getShortMonth: function(d) { var month = d.getMonth(); switch (month) { case 0: return 'Jan'; case 1: return 'Feb'; case 2: return 'Mar'; case 3: return 'Apr'; case 4: return 'May'; case 5: return 'Jun'; case 6: return 'Jul'; case 7: return 'Aug'; case 8: return 'Sep'; case 9: return 'Oct'; case 10: return 'Nov'; case 11: return 'Dec'; } }, getDayOfWeek: function(d) { var dow = d.getDay(); switch (dow) { case 0: return 'Sunday'; case 1: return 'Monday'; case 2: return 'Tuesday'; case 3: return 'Wednesday'; case 4: return 'Thursday'; case 5: return 'Friday'; case 6: return 'Saturday'; } }, getWeekArray: function(d) { var dayArray = []; var daysInMonth = this.getDaysInMonth(d); var daysInWeek; var emptyDays; var firstDayOfWeek; var week; var weekArray = []; for (let i = 1; i <= daysInMonth; i++) { dayArray.push(new Date(d.getFullYear(), d.getMonth(), i)); } while (dayArray.length) { firstDayOfWeek = dayArray[0].getDay(); daysInWeek = 7 - firstDayOfWeek; emptyDays = 7 - daysInWeek; week = dayArray.splice(0, daysInWeek); for (let i = 0; i < emptyDays; i++) { week.unshift(null); } weekArray.push(week); } return weekArray; }, format: function(date) { var m = date.getMonth() + 1; var d = date.getDate(); var y = date.getFullYear(); return m + '/' + d + '/' + y; }, isEqualDate: function(d1, d2) { return d1 && d2 && (d1.getFullYear() === d2.getFullYear()) && (d1.getMonth() === d2.getMonth()) && (d1.getDate() === d2.getDate()); }, isBeforeDate: function(d1, d2) { var date1 = this.cloneAsDate(d1); var date2 = this.cloneAsDate(d2); return (date1.getTime() < date2.getTime()); }, isAfterDate: function(d1, d2) { var date1 = this.cloneAsDate(d1); var date2 = this.cloneAsDate(d2); return (date1.getTime() > date2.getTime()); }, isBetweenDates: function(dateToCheck, startDate, endDate) { return (!(this.isBeforeDate(dateToCheck, startDate)) && !(this.isAfterDate(dateToCheck, endDate))); }, monthDiff: function(d1, d2) { var m; m = (d1.getFullYear() - d2.getFullYear()) * 12; m += d1.getMonth(); m -= d2.getMonth(); return m; }, yearDiff: function(d1, d2) { return ~~(this.monthDiff(d1, d2) / 12); } }