可动态配置的GetCurrentWeekDates与GetCurrentMonthDates日期获取函数开发需求
Hey there, let's tackle these two date retrieval functions you need. I'll walk through each one with clear explanations, parameter breakdowns, example calls, and working code snippets to match your exact requirements.
This function fetches dates for the target week, with full flexibility to set any day as the week's start (Sunday, Monday, or any other day).
Parameters
weekStartDay(NUMBER): Numeric identifier for the week's starting day (your examples use1to represent Sunday).callingday(STRING/Date): The reference date used to determine which week we're targeting (formatted asDD/MM/YYYYin your examples).
Example Calls & Expected Results
- Call:
GetCurrentWeekDates(1, '10/04/2022')(1 = Sunday)Expected Result:
10/04/2022 11/04/2022 12/04/2022 13/04/2022 14/04/2022 15/04/2022 16/04/2022 - Call:
GetCurrentWeekDates(1, '11/04/2022')(1 = Sunday)Expected Result:
11/04/2022 12/04/2022 13/04/2022 14/04/2022 15/04/2022 16/04/2022 - Call:
GetCurrentWeekDates(1, '14/04/2022')(1 = Sunday)Expected Result:
14/04/2022 15/04/2022 16/04/2022
Implementation Example (JavaScript)
function GetCurrentWeekDates(weekStartDay, callingday) { // Parse input date (assuming DD/MM/YYYY format) const [day, month, year] = callingday.split('/').map(Number); const referenceDate = new Date(year, month - 1, day); // Map custom week start day to JavaScript's Date.getDay() (0=Sunday, 6=Saturday) const adjustedStartDay = weekStartDay === 7 ? 6 : weekStartDay - 1; // Calculate the start of the target week const dayOffset = referenceDate.getDay() - adjustedStartDay; const weekStartDate = new Date(referenceDate); weekStartDate.setDate(referenceDate.getDate() - dayOffset); // Generate dates from week start to reference date const dateList = []; let currentDate = new Date(weekStartDate); while (currentDate <= referenceDate) { // Format date as DD/MM/YYYY const formattedDay = String(currentDate.getDate()).padStart(2, '0'); const formattedMonth = String(currentDate.getMonth() + 1).padStart(2, '0'); dateList.push(`${formattedDay}/${formattedMonth}/${currentDate.getFullYear()}`); currentDate.setDate(currentDate.getDate() + 1); } return dateList.join(' '); } // Test the function with your examples console.log(GetCurrentWeekDates(1, '10/04/2022')); console.log(GetCurrentWeekDates(1, '11/04/2022')); console.log(GetCurrentWeekDates(1, '14/04/2022'));
This function retrieves dates from a starting point (callingday) to the last day of a specified month.
Parameters
monthNumber(NUMBER): Numeric identifier for the target month (e.g.,4represents April).callingday(STRING/Date): The starting date for the list, which also determines the target year (formatted asDD/MM/YYYY).
Example Calls & Expected Results
- Call:
GetCurrentMonthDates(4, '01/04/2022')(4 = April)Expected Result:
01/04/2022 02/04/2022 …… 30/04/2022 - Call:
GetCurrentMonthDates(4, '16/04/2022')(4 = April)Expected Result:
16/04/2022 17/04/2022 …… 30/04/2022
Implementation Example (JavaScript)
function GetCurrentMonthDates(monthNumber, callingday) { // Parse input date (assuming DD/MM/YYYY format) const [day, _, year] = callingday.split('/').map(Number); const referenceDate = new Date(year, monthNumber - 1, day); // Get the last day of the target month const lastDayOfMonth = new Date(referenceDate.getFullYear(), monthNumber, 0).getDate(); // Generate dates from reference date to month end const dateList = []; let currentDate = new Date(referenceDate); while (currentDate.getMonth() === monthNumber - 1) { // Format date as DD/MM/YYYY const formattedDay = String(currentDate.getDate()).padStart(2, '0'); const formattedMonth = String(currentDate.getMonth() + 1).padStart(2, '0'); dateList.push(`${formattedDay}/${formattedMonth}/${currentDate.getFullYear()}`); currentDate.setDate(currentDate.getDate() + 1); } return dateList.join(' '); } // Test the function with your examples console.log(GetCurrentMonthDates(4, '01/04/2022')); console.log(GetCurrentMonthDates(4, '16/04/2022'));
内容的提问来源于stack exchange,提问作者Rajesh

