【JavaScript】JavaScript、Java 计算两个日期时间内的每一周,不足一周的按照一周计算

计算两个日期时间内的每一周,不足一周的按照一周计算

JavaScript

function splitWeeks(startDate, endDate) {
  let start = new Date(startDate);
  let end = new Date(endDate);

  let weeks = [];

  let currentDate = new Date(start);
  let weekStart = new Date(currentDate);
  let weekEnd = new Date(currentDate);
  weekEnd.setDate(currentDate.getDate() + (7 - currentDate.getDay()));

  while (currentDate <= end) {
    if (weekEnd > end) {
      weekEnd = new Date(end);
    }

    let weekDates = [];
    let date = new Date(weekStart);
    while (date <= weekEnd) {
      let formattedDate = formatDate(date);
      let dayOfWeek = getDayOfWeek(date);
      weekDates.push({ date: formattedDate, weekday: dayOfWeek });
      date.setDate(date.getDate() + 1);
    }

    weeks.push(weekDates);

    currentDate.setDate(currentDate.getDate() + (7 - currentDate.getDay() + 1));
    weekStart = new Date(currentDate);
    weekEnd = new Date(currentDate);
    weekEnd.setDate(currentDate.getDate() + (7 - currentDate.getDay()));
  }

  return weeks;
}

function formatDate(date) {
  let year = date.getFullYear();
  let month = (date.getMonth() + 1).toString().padStart(2, "0");
  let day = date.getDate().toString().padStart(2, "0");

  return `${year}-${month}-${day}`;
}

function getDayOfWeek(date) {
  let dayOfWeek = date.getDay();
  let weekdays = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"];
  return weekdays[dayOfWeek];
}

// 示例用法
let startDate = "2023-09-19";
let endDate = "2023-10-15";

let result = splitWeeks(startDate, endDate);
console.log(result);

Java

package com.ruoyi.business.utils;

import com.ruoyi.business.vo.XData;
import com.ruoyi.business.vo.YData;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;

/**
 * @author Tellsea
 * @date 2023/9/15
 */
public class CourseUtils {

    /**
     * 纵坐标数据
     *
     * @return
     */
    public static List<YData> getYData() {
        List<YData> list = new ArrayList<>();
        list.add(new YData().setValue(1L).setStart("08:00").setEnd("08:45").setName("第一节课"));
        list.add(new YData().setValue(2L).setStart("08:55").setEnd("09:40").setName("第二节课"));
        list.add(new YData().setValue(3L).setStart("09:55").setEnd("10:40").setName("第三节课"));
        list.add(new YData().setValue(4L).setStart("10:50").setEnd("11:35").setName("第四节课"));
        list.add(new YData().setValue(5L).setStart("11:45").setEnd("12:30").setName("第五节课"));
        list.add(new YData().setValue(6L).setStart("13:30").setEnd("14:15").setName("第六节课"));
        list.add(new YData().setValue(7L).setStart("14:25").setEnd("15:10").setName("第七节课"));
        list.add(new YData().setValue(8L).setStart("15:25").setEnd("16:10").setName("第八节课"));
        list.add(new YData().setValue(9L).setStart("16:20").setEnd("17:05").setName("第九节课"));
        list.add(new YData().setValue(10L).setStart("17:15").setEnd("18:00").setName("第十节课"));
        list.add(new YData().setValue(11L).setStart("18:30").setEnd("19:15").setName("第十一节课"));
        list.add(new YData().setValue(12L).setStart("19:25").setEnd("20:10").setName("第十二节课"));
        list.add(new YData().setValue(13L).setStart("20:20").setEnd("21:05").setName("第十三节课"));
        return list;
    }

    /**
     * 横坐标数据
     * 获取时间段内的每一天
     *
     * @param startDate
     * @param endDate
     * @param week
     * @return
     */
    public static List<XData> getWeekList(String startDate, String endDate, int week) {
        List<List<XData>> weekList = getWeekList(startDate, endDate);
        if (!CollectionUtils.isEmpty(weekList)) {
            return weekList.get(week);
        }
        return Collections.emptyList();
    }

    /**
     * 计算两个日期时间内的每一周,不足一周的按照一周计算
     *
     * @param startDate
     * @param endDate
     * @return
     */
    public static List<List<XData>> getWeekList(String startDate, String endDate) {
        List<List<XData>> weeks = new ArrayList<>();

        LocalDate start = LocalDate.parse(startDate);
        LocalDate end = LocalDate.parse(endDate);

        LocalDate current = start;
        LocalDate weekStart = start.with(java.time.temporal.TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY));
        LocalDate weekEnd = start.with(java.time.temporal.TemporalAdjusters.nextOrSame(java.time.DayOfWeek.SUNDAY));

        while (current.isBefore(end) || current.isEqual(end)) {
            if (weekEnd.isAfter(end)) {
                weekEnd = end;
            }

            List<XData> weekDates = new ArrayList<>();
            LocalDate date = weekStart;
            while (date.isBefore(weekEnd) || date.isEqual(weekEnd)) {
                String formattedDate = formatDate(date);
                String dayOfWeek = getDayOfWeek(date);
                weekDates.add(new XData(formattedDate, dayOfWeek));
                date = date.plusDays(1);
            }

            weeks.add(weekDates);

            current = current.plusDays(7 - current.getDayOfWeek().getValue() + 1);
            weekStart = current.with(java.time.temporal.TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY));
            weekEnd = current.with(java.time.temporal.TemporalAdjusters.nextOrSame(java.time.DayOfWeek.SUNDAY));
        }

        return weeks;
    }

    public static String formatDate(LocalDate date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        return date.format(formatter);
    }

    public static String getDayOfWeek(LocalDate date) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("EEEE", Locale.CHINA);
        return date.format(formatter);
    }
}

你可能感兴趣的:(JavaScript,javascript,前端,开发语言)