【问题标题】:Get the date from day number of week Javascript从周数Javascript中获取日期
【发布时间】:2021-08-20 03:28:44
【问题描述】:

我的数据库中有一个模型,其中包含一个名为“AvailableDays”[0...6] 的数组。 0 = 星期日 & 6 = 星期六。我希望将一周中的这一天数转换为当前一周的日期。

例如,这是分解的逻辑

  1. 检索可用日期列表 (const availableDays = [0,2,4,6])
  2. 获取当前日期 (const today = new Date('2021-08-20');)
  3. 将日期数字转换为日期 (output =['15-08-2021', '17-08-2021', '19-08-2021', '21-08-2021'])

【问题讨论】:

  • 我实际上建议更改您的数据库设计。与其存储星期几,不如存储实际的正确日期。
  • @TimBiegeleisen 我假设他有某种日程安排应用程序,其中可用日期重复每周,他想找到当前一周的日期。

标签: javascript date


【解决方案1】:

您可以做的是从给定的Date 实例中获取星期几,然后计算出您的可用日期的偏移量。

然后从给定日期减去该偏移量(以天为单位)以产生结果。

const transformDate = (date, day) => {
  const offset = date.getDay() - day
  
  const d = new Date(date)
  d.setDate(d.getDate() - offset)
  return d
}

const availableDays = [0,2,4,6]
const today = new Date("2021-08-20")

console.log(availableDays.map(day => transformDate(today, day)))

【讨论】:

  • Stack Snippets 似乎坏了(至少对我来说),所以如果示例无法运行,我们深表歉意
  • 是的,但问题现在已解决,您在 console.log 方法中错过了 ) :)
  • @Phil 非常感谢,我自己能够解决这个问题,但你的回答比我的更完整。感谢您的帮助!
  • @TomDickson 基本上是一样的。我很高兴你能自己弄清楚
【解决方案2】:

能够自己解决这个问题。我现在可以将其包装到 availableDates.map() 中,并使用以下逻辑返回一个日期数组。

var availableDay = 0
var d = new Date(),
    day = d.getDay(), // 0 ... 6
    calcAvailableDay = day-availableDay,
    diff = d.getDate() - calcAvailableDay,
    output = new Date(d.setDate(diff));
console.log(output)

【讨论】:

    【解决方案3】:

    您可以生成以周为单位的所有日期,然后使用 availableDays 获取日期。

    const getWeekDays = (current) => {
        current.setDate((current.getDate() - current.getDay() - 1));
        return Array.from({ length: 7 }, (_, i) => {
          current.setDate(current.getDate() + 1)
          return new Date(current).toLocaleDateString('en-CA');
        });
      },
      today = new Date('2021-08-20'),
      weekDays = getWeekDays(today),
      availableDays = [0, 2, 4, 6],
      availableDates = availableDays.map(day => weekDays[day]);
    console.log(availableDates);

    【讨论】:

    • 在输入端,'2021-08-20' 被解析为 UTC。然后在getWeekDays中,toISOString用于获取日期部分。因此,偏移量为负的用户会看到比预期晚一天的日期(8 月 16 日、18 日、20 日、22 日),而偏移量为正的用户会看到预期的日期(15、17、19、21)。此外,获取所有工作日的数组然后过滤掉不需要的日期有点低效。
    【解决方案4】:

    JavaScript getDay 方法根据当地时间返回指定日期的星期几,其中 0 代表星期日。

    因此,您要做的就是将此索引与您的 availableDays 值联系起来。

    逻辑

    • 获取当前日期、月份、年份和今天日期的索引。
    • 循环遍历availableDays 数组,并使用getDay 值计算的当前日期与数组中指定的日期值之间的差值创建新日期。
    • 利用一些逻辑以指定格式重新表示这些日期对象。我得到了this 帖子的支持来格式化您的日期字符串。

    const availableDays = [0,2,4,6];
    const today = new Date();
    const currentDay = today.getDay();
    const currentDate = today.getDate();
    const currentMonth = today.getMonth();
    const currentYear = today.getFullYear();
    formatDateToString = (date) => String(date.getDate()).padStart(2, '0') + '-' + String(date.getMonth() + 1).padStart(2, '0') + '-' + date.getFullYear();
    const output = availableDays.map((day) => formatDateToString(new Date(currentYear, currentMonth, currentDate - (currentDay - day))));
    console.log(output);

    【讨论】:

      猜你喜欢
      • 2013-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-19
      • 2017-08-09
      • 1970-01-01
      • 2014-04-21
      相关资源
      最近更新 更多