Skip to content

Instantly share code, notes, and snippets.

@dinizgb
Last active June 30, 2023 14:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dinizgb/8d1e9b45ff57ce2fb09ba96fe17a4d17 to your computer and use it in GitHub Desktop.
Save dinizgb/8d1e9b45ff57ce2fb09ba96fe17a4d17 to your computer and use it in GitHub Desktop.
Javascript function to add or decrease days to a date.
/**
* Function to add or decrease days to a date
* @param {string} date - With the date (With at least yyyy-mm-dd date format)
* @param {number} days - With the days
* @param {boolean} action - True to increase days or false to decrease days
* @returns {string} With the the new date
*/
export const addOrDecreaseDaysToDate = (date: string, days: number, action: boolean): string => {
const result = new Date(date);
action ? result.setDate(result.getDate() + days) : result.setDate(result.getDate() - days);
return result.toISOString();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment