I've got this function getFutureDates that helps me render days based on the current day…
- User
I've got this function getFutureDates that helps me render days based on the current day so that the next 2 weeks in business days are always shown, but when it's a Saturday is renders only the next 4 days. How can I fix it?
export const addBusinessDayRange = (date: Date) => addBusinessDays(date, 10 - getDay(date));
export const getFutureDates = () => { const now = new Date('2023-05-27'); const twoMondaysFromNow = startOfDay(addDays(nextMonday(now), 7)); const fourMondaysFromNow = startOfDay(addDays(twoMondaysFromNow, 14)); const twoFridaysFromTwoMondays = endOfDay( addBusinessDayRange(twoMondaysFromNow) ); const twoFridaysFromFourMondays = endOfDay( addBusinessDayRange(fourMondaysFromNow) ); const today = startOfDay(now); const twoFridaysFromToday = endOfDay(addBusinessDayRange(today));
return { twoMondaysFromNow, fourMondaysFromNow, twoFridaysFromFourMondays, twoFridaysFromTwoMondays, today, twoFridaysFromToday }; };
- Assistant
To fix the function to show the next 2 weeks in business days even when it's a Saturday, you can make the following modifications:
- Remove the condition that checks if it's a Saturday and only renders the next 4 days.
- Instead of setting
twoFridaysFromTwoMondaysandtwoFridaysFromFourMondays, calculate the dates based on the number of business days. - Modify the
addBusinessDayRangefunction to return the correct number of business days based on the current day.
Here's the modified code:
export const addBusinessDayRange = (date: Date) => { const dayOfWeek = getDay(date); const daysToAdd = dayOfWeek === 0 ? 1 : dayOfWeek <= 5 ? 10 : 9; return addBusinessDays(date, daysToAdd); }; export const getFutureDates = () => { const now = new Date(); const twoWeeksFromNow = startOfDay(addDays(now, 14)); const twoFridaysFromNow = endOfDay(addBusinessDayRange(now)); const twoFridaysFromTwoWeeks = endOfDay(addBusinessDayRange(twoWeeksFromNow)); const today = startOfDay(now); const twoFridaysFromToday = endOfDay(addBusinessDayRange(today)); return { twoWeeksFromNow, twoFridaysFromNow, twoFridaysFromTwoWeeks, today, twoFridaysFromToday }; }; - User
How can I improve this?
export const getFutureDates = () => { const now = new Date(); const twoMondaysFromNow = startOfDay(addDays(nextMonday(now), 7)); const fourMondaysFromNow = startOfDay(addDays(twoMondaysFromNow, 14)); const twoFridaysFromTwoMondays = endOfDay( addBusinessDayRange(twoMondaysFromNow) ); const twoFridaysFromFourMondays = endOfDay( addBusinessDayRange(fourMondaysFromNow) ); const today = startOfDay(now); const twoFridaysFromToday = endOfDay(addBusinessDayRange(today));
return { twoMondaysFromNow, fourMondaysFromNow, twoFridaysFromFourMondays, twoFridaysFromTwoMondays, today, twoFridaysFromToday }; };
- Assistant
Here are a few suggestions to improve the
getFutureDatesfunction:- Make the function more generic by accepting a parameter for the number of weeks to look ahead, instead of hardcoding two weeks.
- Use variables/constants to avoid repetitive code, such as the
addDaysandstartOfDayfunctions. - Rename the keys in the returned object to be more descriptive.
Here's an updated version of the function:
export const getFutureDates = (numWeeks = 2) => { const now = new Date(); const startDate = startOfDay(now); const endDate = startOfDay(addWeeks(now, numWeeks)); const mondayDates = []; const fridayDates = []; // Get the Monday dates for the range for (let i = 0; i < numWeeks * 2; i += 2) { const monday = startOfDay(addDays(nextMonday(startDate), i)); mondayDates.push(monday); } // Get the Friday dates for the range for (let i = 0; i < numWeeks * 2; i += 2) { const friday = endOfDay(addBusinessDayRange(mondayDates[i])); fridayDates.push(friday); } // Get the date for today and the next Friday const today = startDate; const nextFriday = endOfDay(addBusinessDayRange(today)); return { startDate, endDate, mondayDates, fridayDates, today, nextFriday, }; }; - User
I have this function:
import { deleteOfficeChatByName, getOfficeChatsToArchive } from '@/services/officeChat'; import { WebClient } from '@slack/web-api'; import { Callback, Context, Handler } from 'aws-lambda';
const webClient = new WebClient(process.env.SLACK_BOT_TOKEN);
export const officeChatArchiver: Handler = async ( event: any, context: Context, callback: Callback ) => { try { // Query the database for all channel names to archive const channels = await getOfficeChatsToArchive();
if (!channels.length) { callback(null, { statusCode: 200, body: 'No channels to archive' }); return; } // Archive each channel using the Slack Web API for (const channel of channels) { await webClient.conversations .info({ channel: channel.channel }) .then(async (info) => { if (!info.channel?.is_archived) { await webClient.conversations.archive({ channel: channel.channel }); console.log(`Channel ${channel.name} archived`); } else { console.log(`Channel ${channel.name} already archived`); } await deleteOfficeChatByName(channel.name); }); } callback(null, { statusCode: 200, body: 'Channels archived successfully.' }); - Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.