list of way, we can generate calendar (month, week, or other views)
Last active
July 2, 2021 18:14
-
-
Save mikoloism/be036e5a2888bd66f62ddb8f0f861879 to your computer and use it in GitHub Desktop.
Calendar Generator
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
// https://github.com/sjlu/calendarjs/blob/master/index.js | |
// [TIPS]: need to configing and standardization |
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
import moment from 'moment'; | |
class YKDate { | |
constructor(date) { | |
this.date = date || moment(); | |
} | |
createDay() { | |
// coming soon | |
} | |
createWeek(format) { | |
let _date = this.date ? moment(this.date) : moment(); | |
let first = _date.clone().startOf("week"); | |
let last = _date.clone().endOf("week"); | |
let day = first.clone().subtract(1, "day"); | |
let _week = []; | |
while (day.isBefore(last, "day")) { | |
let _day = day.add(1, "day").clone(); | |
_week.push(format ? _day.format(format) : _day); | |
} | |
return _week; | |
} | |
createMonth(format) { | |
let _date = this.date ? moment(this.date) : moment(); | |
let first = _date.clone().startOf("month").startOf("week"); | |
let last = _date.clone().endOf("month").endOf("week"); | |
let day = first.clone().subtract(1, "day"); | |
let _month = []; | |
while (day.isBefore(last, "day")) { | |
_month.push( | |
[...Array(7).keys()].map(() => { | |
let _day = day.add(1, "day").clone(); | |
return format ? _day.format(format) : _day; | |
}) | |
); | |
} | |
return _month; | |
} | |
createYear(format) { | |
// coming soon | |
} | |
generate(view, format) { | |
switch (String(view).toUpperCase()) { | |
case `DAY`: | |
return this.createDay(format); | |
case `WEEK`: | |
return this.createWeek(format); | |
case `MONTH`: | |
default: | |
return this.createMonth(format); | |
} | |
} | |
} | |
export default YKDate |
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
// npm install moment --save | |
import moment from 'moment'; | |
const MONTH = (date, format) => { | |
let _date = date ? moment(date) : moment(); | |
let first = _date.clone().startOf('month').startOf('week'); | |
let last = _date.clone().endOf('month').endOf('week'); | |
let day = first.clone().subtract(1, 'day'); | |
let _month = []; | |
while(day.isBefore(last, 'day')){ | |
_month.push([...Array(7).keys()].map(() => { | |
let _day = day.add(1, 'day').clone(); | |
return format ? _day.format(format) : _day; | |
})); | |
} | |
return _month; | |
}; | |
const WEEK = (date, format) => { | |
let _date = date ? moment(date) : moment(); | |
let first = _date.clone().startOf('week'); | |
let last = _date.clone().endOf('week'); | |
let day = first.clone().subtract(1, 'day'); | |
let _week = []; | |
while(day.isBefore(last, 'day')){ | |
let _day = day.add(1, 'day').clone(); | |
_week.push(format ? _day.format(format) : _day); | |
} | |
return _week; | |
}; | |
const DAY = (date, format) => { | |
return `coming soon...`; | |
}; | |
const generator =(view, { date, format })=> { | |
switch (String(view).toUpperCase()) { | |
case `DAY`: | |
return DAY(date, format); | |
case `WEEK`: | |
return WEEK(date, format); | |
case `MONTH`: | |
default: | |
return MONTH(date, format); | |
} | |
}; | |
export { | |
MONTH as RangeMonth, | |
WEEK as RangeWeek, | |
DAY as RangeDay, | |
}; | |
export default generator; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment