Last active
October 19, 2021 11:50
-
-
Save hmetgundogdu/7b8e3e08375e6e3eca2b2919d97653d7 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 TimeType = { Day: 1, Week: 2, Mounth: 3, Year: 4 } | |
| /** | |
| * Adds time as defined a type day, week, mounth and year to given date | |
| * @param {TimeType} Time type will add | |
| * @param {Date} How many adds times to value | |
| * @param {boolean | undefined} if it is true, time will add as negative way. | |
| * @returns {Date} New value | |
| */ | |
| function addTimeToDate(timeType, times, date, negative = false) { | |
| const newDate = new Date(date) | |
| const computation = (value1, value2) => (negative ? value1 - value2 : value1 + value2) | |
| switch(timeType) { | |
| case 1: newDate.setDate(computation(newDate.getDate(), times)) | |
| return newDate; | |
| case 2: newDate.setDate(computation(newDate.getDate(), times * 7)) | |
| return newDate; | |
| case 3: newDate.setMonth(computation(newDate.getMonth(), times)) | |
| return newDate; | |
| case 4: newDate.setFullYear(computation(newDate.getFullYear(), times)) | |
| return newDate; | |
| } | |
| } | |
| Object.freeze(TimeType) | |
| module.exports = { TimeType, addTimeToDate } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment