Last active
May 8, 2018 23:43
-
-
Save adover/4c286a2deea11e80afdc904cc6b88e3f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const config = require('../config/config.json'); | |
const moment = require('moment'); | |
const debug = require('debug')('DateHelper'); | |
class Date { | |
/** | |
* Gets the day of the week | |
* @param {[type]} date [description] | |
* @return moment date | |
*/ | |
constructor() { | |
this.date = config.date; | |
} | |
// eslint-disable-next-line class-methods-use-this | |
getDayOfWeek(date = this.date, format = 'DD-MM-YYYY') { | |
return moment(date, format).isValid() ? moment(date, format).format('dddd') : false; | |
} | |
/** | |
* Checks date inputs and returns dates | |
*/ | |
// eslint-disable-next-line class-methods-use-this | |
getStartAndEndDate(startDate = false, endDate = false) { | |
const end = endDate ? moment(endDate) : moment(config.date); | |
const start = startDate ? moment(startDate) : end.clone().subtract(13, 'days'); | |
return { | |
start, | |
end, | |
}; | |
} | |
/** | |
* Gets an array of dates between two days | |
*/ | |
// eslint-disable-next-line class-methods-use-this | |
getDaysBetweenTwoDates(start, end, singularDay) { | |
const dates = []; | |
const now = moment(start); | |
const momentEnd = moment(end); | |
if (!start || !end) { | |
return false; | |
} | |
if (momentEnd.isSameOrBefore(start)) { | |
throw new Error('End date is the same or before the start date. This would cause an infinite loop'); | |
} | |
while (now.isSameOrBefore(end)) { | |
if (!singularDay || now.day() == singularDay) { | |
dates.push([now.format(), now.format('dddd'), now]); | |
} | |
now.add(1, 'days'); | |
} | |
return dates; | |
} | |
// eslint-disable-next-line class-methods-use-this | |
getMonth(date = this.date) { | |
const end = moment(date) | |
.endOf('month') | |
.format('YYYY-MM-DD'); | |
const start = moment(date) | |
.startOf('month') | |
.format('YYYY-MM-DD'); | |
const name = moment(date).format('MMMM'); | |
return { start, end, name }; | |
} | |
} | |
module.exports = new Date(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment