본문 바로가기
Study(매일매일한걸음씩)/Web(html,css,js)

input date오늘 날짜로 설정하기

by 여유러운백수삶개발자 2022. 12. 13.
  • html에서 input type date 로 했을때 오늘 날짜로 설정하기

 

https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Date

 

Date - JavaScript | MDN

JavaScript Date 객체는 시간의 한 점을 플랫폼에 종속되지 않는 형태로 나타냅니다. Date 객체는 1970년 1월 1일 UTC(협정 세계시) 자정과의 시간 차이를 밀리초로 나타내는 정수 값을 담습니다.

developer.mozilla.org

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <span>시작날짜</span>
    <input type="date" id="start_Day">
    <p></p>
    <span>마감날짜</span>
    <input type="date" id="end_Day">
    <p></p>
    <span>일한날짜</span>
    <input type="date" id="work_Day">
    <script>
        const dateControl1 = document.querySelector('#start_Day');
        const dateControl2 = document.querySelector('#end_Day');
        const dateControl3 = document.querySelector('#work_Day');

        date = new Date();
        year = date.getFullYear();
        month = date.getMonth() + 1;
        day = date.getDate();
        start_day_value = year + "-" + month + "-" + day;

        day = date.getDate() + 3;
        end_day_value = year + "-" + month + "-" + day;

        day = date.getDate() + 2;
        work_day_value = year + "-" + month + "-" + day;

        console.log('date.now = ', Date.now())
        console.log('date.parse = ', Date.parse(date))
        console.log('date.UTC = ', Date.UTC(11))
        console.log('date = ', date)
        console.log('date.getFullyear = ', year)
        console.log('date.GetMonth =', month)
        console.log('date.getDay = ', day)
        console.log('test = ', start_day_value)
        console.log('datatype(start_day_value) =',typeof(start_day_value))

        dateControl1.value = start_day_value;
        dateControl2.value = end_day_value;
        dateControl3.value = work_day_value;
    </script>
</body>

</html>

결과값(오늘은 12월13일)
콘솔 출력

 


NOTE.

  1. Date()로 오늘 날짜를 불러온다.
  2. Month는 0~11이라서 1을 더해서 계산한다.(1월이 0 ,12월이 11임)
  3. 출력 날짜는 타입은 string이다.
  4. start_day_value = year + "-" + month + "-" + day; 라고 한 목적은 input date 방식가 맞아야지 입력이 됨
    1. 처음에는 달,일,년 했는데 안됨
  5. #start_day의 #은 아이디를 뜻한다.(저도 초보라 기록해둠)
  6.  

index.html
0.00MB

댓글