You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

可动态配置的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.


1. GetCurrentWeekDates Function

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 use 1 to represent Sunday).
  • callingday (STRING/Date): The reference date used to determine which week we're targeting (formatted as DD/MM/YYYY in 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'));

2. GetCurrentMonthDates Function

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., 4 represents April).
  • callingday (STRING/Date): The starting date for the list, which also determines the target year (formatted as DD/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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 18:17:34