(JS) 날짜 구하기

호두파파·2021년 2월 3일
0

호두파파 JS 스터디

목록 보기
7/27

오늘 날짜 구하기


const aa = new Date();

const year = aa.getFullYear();
const month = aa.getMonth();
const date = aa.getDate();
const dayLabel = aa.getDay();

console.log(year)     // 출력: 2021
console.log(month)     // 출력: 1
console.log(date)     // 출력: 3
console.log(dayLabel)     // 출력: 3

console.log(dayLabel) // 출력: 2
요일은 일요일을 시작으로 토요일까지 각각 인덱스 번호로 담겨 출력이 되는 걸 확인할 수 있다. 이것을 문자열로 표현하기 위해서는
배열을 이용하면 된다.

오늘 날짜 요일 구하기

function getTodayLabel() {
  const week = ['일요일', '월요일', '화요일', '수요일', '목요일', '금요일', '토요일'];
  const today = new Date().getDay();
  const todayLabel = week[today];
  
  return todayLabel;
}

console.log(getTodaLabel()); // 수요일

배열을 만들어 요소로 일요일 - 토요일까지 대응하는 문자를 넣어주고, getDate()의 결과값에 맞는 인덱스의 값을 출력하도록 하면 해결할 수 있다.

특정 날짜의 요일 구하기

function getInputDayLabel() {
  const week = ['일요일', '월요일', '화요일', '수요일', '목요일', '금요일', '토요일'];
  const today = new Date('2021-01-03').getDay();
  const todayLabel = week[today];
  
  return todayLabel;
}

console.log(getInputDayLabel()) // 수요일

Advance

입력한 날짜의 해당 달 기준의 주차 구하기

function weekNumberByMonth(dateFormat) {
  const inputDate = new Date(dateFormat);
  
  // 인풋의 년, 월 
  let year = inputDate.getFullYear();
  let month = inputDate.getMonth() + 1;
  
  // 목요일 기준 주차 구하기 
  const weekNumberByThurFnc = (paramdate) => {
    const year = paramDate.getFullYear();
    const month = paramDate.getMonth();
    const date = paramDate.getDate();
    
    // 인풋한 달의 첫 날과 마지막 날의 요일 
    const firstDate = new Date(year, month, 1);
    const lastDate = new Date(year, month+1, 0);
    const firstDayofWeek = fisstDate.getDay() === 0 ? 7 : fistDate.getDay();
    const lasstDayofWeek = lastDate.getDay();
    
    // 인풋한 달의 마지막 일 
    const lastDay = lastDate.getDate();
    
    // 첫 날의 요일이 금, 토, 일이라면 true
    const fistWeekCheck = fistDayOfWeek === 5 || fistDayOfWeek === 6 || fistDayOfWeek ==7;
    const lastWeekCheck = lastDayOfWeek === 1 || lastDayOfWeek === 2 || lastDayOfWeek === 3;
    
    // 해당 달이 총 몇주까지 있는지 
    const lastWeekNo = Math.ceil((fistDayOfWeek -1 + lastDay / 7);
                                 
    // 날짜 기준으로 몇 주차 인지
    let weekNo = Math.ceil((fistDayOfWeek -1 + date) / 7);
    // 인풋한 날짜가 첫 주에 있고 첫 날이 월, 화, 수로 시작한다면 'prev'(전달 마지막 주)
    if (weekNo === 1 && fistWeekCheck) weekNo = 'prev';
    // 인풋한 날짜가 마지막 주에 있고 마지막 날이 월, 화, 수로 끝난다면 'next'(다음달 첫 주)
    else if(weekNo === lastWeekNo && lastWeekCheck) weekNo = 'next'l
    // 인풋한 날짜의 첫 주는 아니지만 첫 날이 월, 화, 수로 시작하면 -1;
    else if (fistWeekCheck) weekNo = weekNo -1 ;
    
    return weekNo;
  };
  
  // 목요일 기준의 주차 
  last weekNo = weekNumberByThurFnc(inputDate);
  
  // 이전달의 마지막 주차일때 
  const afterDate = new Date(year, month01, 0);
  year = month === 1 ? year -1 : year;
  month = month === 1 ? 12 : month - 1;
  weekNo = weekNumberByThurFnc(afterDate);
}
// 다음달의 첫 주차일 때 
if(weekNo === 'next') {
  year = month === 12 ? year+1 : year;
  month = month === 12 ? 1 : month + 1;
  weekNo = 1;
}
  return {
    year, month, weekNo
  }
}

예시

weekNumberByMonth('2018-09-05'); 
// {year: 2018, month: 9, weekNo: 1}
// 2018년 9월 5일은 2018년 9월의 1주차
weekNumberByMonth(new Date(2018,8,5)); 
// {year: 2018, month: 9, weekNo: 1}
// 2018년 9월 5일은 2018년 9월의 1주차
weekNumberByMonth('2014-03-01'); 
// {year: 2014, month: 2, weekNo: 4}
// 2014년 3월 1일은 2014년 2월의 4주차
weekNumberByMonth('2016-01-02'); 
// {year: 2015, month: 12, weekNo: 5}
// 2016년 1월 2일은 2015년 12월의 5주차
weekNumberByMonth(new Date(2018,6,30));
// {year: 2018, month: 8, weekNo: 1}
// 2018년 7월 30일은 2018년 8월 1주차
profile
안녕하세요 주니어 프론트엔드 개발자 양윤성입니다.

0개의 댓글