@ashirbad/js-core
Version:
A set of js core utility functions
105 lines (104 loc) • 3.01 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDatesBetween = exports.getDayName = exports.getFutureDays = void 0;
/**
* Returns an array of future dates starting from today.
*
* @param {number} [numberOfDays=7] - The number of future days to generate
* @returns {Date[]} An array of Date objects representing future dates
*
* @example
* // Get next 3 days
* const dates = getFutureDays(3);
* // Result: [
* // 2024-03-07T00:00:00.000Z,
* // 2024-03-08T00:00:00.000Z,
* // 2024-03-09T00:00:00.000Z
* // ]
*
* @example
* // Default usage (7 days)
* const weekDates = getFutureDays();
* // Returns array of next 7 days
*/
const getFutureDays = (numberOfDays = 7) => {
const arr = Array.from(Array(numberOfDays).keys()).map((item, i) => {
const nextDay = new Date();
const futureDate = nextDay.getDate() + i;
nextDay.setDate(futureDate);
return nextDay;
});
return arr;
};
exports.getFutureDays = getFutureDays;
/**
* Gets the name of a day by its index (0-6) or returns today's name if no index is provided.
*
* @param {number} [dayIndex=new Date().getDay()] - The day index (0 for Sunday, 1 for Monday, etc.)
* @returns {day} The name of the day
*
* @example
* // Get specific day name
* getDayName(1); // "Monday"
* getDayName(0); // "Sunday"
* getDayName(6); // "Saturday"
*
* @example
* // Get today's name (no argument)
* getDayName(); // Returns current day name
*/
const getDayName = (dayIndex = new Date().getDay()) => {
const days = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];
return days[dayIndex];
};
exports.getDayName = getDayName;
/**
* Returns an array of dates between two dates (inclusive or exclusive of end date).
*
* @param {Date} startDate - The start date
* @param {Date} endDate - The end date
* @param {boolean} [includeEndDate=false] - Whether to include the end date in the result
* @returns {Date[]} Array of dates between start and end dates
*
* @example
* // Get dates between (exclusive)
* const dates = getDatesBetween(
* new Date('2024-03-01'),
* new Date('2024-03-05')
* );
* // Result: [
* // 2024-03-01T00:00:00.000Z,
* // 2024-03-02T00:00:00.000Z,
* // 2024-03-03T00:00:00.000Z,
* // 2024-03-04T00:00:00.000Z
* // ]
*
* @example
* // Get dates between (inclusive)
* const dates = getDatesBetween(
* new Date('2024-03-01'),
* new Date('2024-03-05'),
* true
* );
* // Result includes end date (2024-03-05)
*/
const getDatesBetween = (startDate, endDate, includeEndDate) => {
const dates = [];
const currentDate = new Date(startDate);
while (currentDate < endDate) {
dates.push(new Date(currentDate));
currentDate.setDate(currentDate.getDate() + 1);
}
if (includeEndDate)
dates.push(endDate);
return dates;
};
exports.getDatesBetween = getDatesBetween;
;