import { useState, useEffect } from "react"; import { useHistory } from "react-router-dom"; import moment from "moment"; import DatePicker from "./DatePicker.js"; import styles from "./calender.module.scss"; const Calender = ({ selectDate, setSelectDate }) => { const [dateList, setDateList] = useState([]) const [dateArr, setDateArr] = useState([]) const [weekly, setWeekly] = useState([]) const [week, setWeek] = useState(["일", "월", "화", "수", "목", "금", "토"]) const [month, setMonth] = useState({ pre: moment(selectDate).subtract(1, 'months').format('YYYY-MM'), cur: moment(selectDate).format('YYYY-MM'), next: moment(selectDate).add(1, 'months').format('YYYY-MM') }) const history = useHistory() useEffect(() => { setMonth({ ...month, pre: moment(selectDate).subtract(1, 'months').format('YYYY-MM'), cur: moment(selectDate).format('YYYY-MM'), next: moment(selectDate).add(1, 'months').format('YYYY-MM') }) getDate(selectDate) getWeek(selectDate) }, [selectDate]) function getWeek(oneDay) { let dateArr = [] dateArr = Array(7).fill(0).map((n, i) => { let current = moment(oneDay).add((n + i), 'days') return
setSelectDate(current.format('YYYY-MM-DD'))}> {current.isSame(oneDay) || current.format("DD") === "01" ? {current.format('YYYY.MM')} : ""}
{current.format('DD')} {current.isSame(moment().format('YYYY-MM-DD')) ? "오늘" : week[Number(current.format("d"))]}
}) console.log("Dadsa--===", dateArr) setWeekly(dateArr) } function getDate(oneDay) { let dateArr = [] let lastWeek = moment(oneDay).subtract(7, 'days') const nextWeek = moment(oneDay).add(7, 'days') while (lastWeek.isBefore(nextWeek)) { dateArr.push({ date: lastWeek.format('YYYY-MM-DD'), day: lastWeek.format('d') }) lastWeek.add(1, 'days') } const resultArr = dateArr.map(el =>
setSelectDate(el.date)}> {moment(el.date).isSame(oneDay) || el.date.split('-')[2] === "01" ? {moment(el.date).format('YYYY.MM')} : ""}
{moment(el.date).format('DD')} {moment(el.date).isSame(moment().format('YYYY-MM-DD')) ? "오늘" : week[el.day]}
) setDateList(resultArr) } function preWeek() { // let dateArr = [] // dateArr = Array(7).fill(0).map((n, i) => { // let current = moment(oneDay).add((n + i), 'days') // return
setSelectDate(current)}> // {current.isSame(oneDay) || current.format("DD") === "01" ? {current.format('YYYY.MM')} : ""} //
// {current.format('DD')} // {current.isSame(oneDay) ? "오늘" : week[Number(current.format("d"))]} //
//
// }) // console.log("Dadsa--===", dateArr) // setWeekly(dateArr) } function nextWeek() { } return ( <> {console.log("date==", selectDate)}
{weekly.map(el => el)}
) } export default Calender