달력

5

« 2025/5 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
2024. 5. 27. 11:13

javascript 날짜 함수 카테고리 없음2024. 5. 27. 11:13

/**

* numberComma(n)

* 숫자에 콤마 추가함(12345 --> 12,345).

*/

function numberComma(n) {

//-- 정규식

var reg = /(^[+-]?\d+)(\d{3})/;

//-- 숫자를 문자열로 변환

n += '';

while (reg.test(n))

n = n.replace(reg, '$1' + ',' + '$2');

return n;

}

 

/*yyyyMMdd 형식의 날짜 문자열을 입력 받아 연(year)을 증감한다.*/

function addYear( dateStr, year ) {

dateStr = rmNotNumChar(dateStr);

return addYearMonthDay( dateStr, year, 0, 0 );

}

 

/*yyyyMMdd 형식의 날짜 문자열을 입력 받아 일(day)을 증감한다.*/

function addDay( dateStr, day ) {

dateStr = rmNotNumChar(dateStr);

return addYearMonthDay( dateStr, 0, 0, day );

}

 

function addYearMonthDay( source, year, month, day) {

if (isNaN(source) || source.length < 8) {

return "";

}

var yyyy = source.substr(0, 4);

var mm = parseInt(source.substr(4, 2));

var dd = source.substr(6, 2);

 

var dateStr = new Date(yyyy,(mm-1),dd);

var addDay;

if (year != 0) {

//console.log('@@@ year :: '+year);

addDay = new Date( dateStr.setFullYear( dateStr.getFullYear() + year ) );

}

if (month != 0) {

//console.log('@@@ month :: '+month);

addDay = new Date( dateStr.setMonth( dateStr.getMonth() + month ) );

}

if (day != 0) {

//console.log('@@@ day :: '+day);

addDay = new Date( dateStr.setDate( dateStr.getDate() + day ) );

}

//console.log('@@@ addDay :: '+getDateYMD(addDay));

return getDateYMD(addDay);

}

 

/* 입력된 date객체를 날짜형식으로 yyyymmdd */

function getDateYMD( source ) {

var now;

if( empty(source) ){

now = new Date();

}else{

now = new Date( source );

}

 

var year = now.getFullYear();

var month = ("0" + (1 + now.getMonth())).slice(-2);

var day = ("0" + now.getDate()).slice(-2);

 

return year + month + day;

}

 

/* 입력된 str객체를 날짜형식으로 YYYY-MM-DD */

function getDateFormatYMD( source ) {

source = rmNotNumChar(source);

if (isNaN(source) || source.length < 8) {

return "";

}

var yyyy = source.substr(0, 4);

var mm = source.substr(4, 2);

var dd = source.substr(6, 2);

 

var dateStr = new Date(yyyy,(mm-1),dd);

 

var year = dateStr.getFullYear();

var month = ("0" + (1 + dateStr.getMonth())).slice(-2);

var day = ("0" + dateStr.getDate()).slice(-2);

 

return year + "-" + month + "-" + day;

}

 

/* 해당달의 마지막 날짜 구하기 */

function getlastDay( source ) {

source = source.replace(/\D/ig, "");

if (isNaN(source) || source.length < 6) {

return "";

}

var year = source.substr(0, 4);

var month = source.substr(4, 2);

var newDate = new Date(year, month, 0);

 

var newYear = newDate.getFullYear();

var newMonth = newDate.getMonth()+1;

var newDay = newDate.getDate();

//console.log(newMonth);

 

if (newMonth.toString().length < 2) newMonth = "0" + newMonth;

if (newDay.toString().length < 2) newDay = "0" + newDay;

 

return newYear.toString() + newMonth.toString() + newDay.toString();

}

:
Posted by 상문이

onchange가 select 태그 등에 동작하지 않는 원인은 select 태그를 클릭해서 값을 선택하지 않고,
select 태그의 value 프로퍼티의 값을 스크립트를 사용해 업데이트 하는 경우입니다. 
onchange 이벤트는 해당 element의 blur 여부에 따라 동작하기 때문에 값이 갱신되지 않습니다.

 

출처 : https://coding-restaurant.tistory.com/317

 

[JS] onchange 이벤트 사용, value 가져오기

자바스크립트 onchange는 작성한 JS를 통해 변화가 일어났는지를 감지합니다. jQuery change 이벤트와 비슷합니다. onchange 이벤트의 사용 방법은 아래와 같습니다. 첫 번째는 html에서 사용할 때, 나머지

coding-restaurant.tistory.com

 

 

:
Posted by 상문이
2024. 5. 14. 11:27

javascript 한달전 후 날짜구하기 카테고리 없음2024. 5. 14. 11:27

//일정목록
        if( empty(currentDate) ) {
            currentDate = getTodayYMD();
        }
//이전 버튼
        $('button[id=preBtn]').on('click', function(){
            /*한달전 날짜 구하기*/
            currentDate = getPreMonth(currentDate.substr(0,4),currentDate.substr(4,2),currentDate.substr(6,2));
            var calendarEl1 = document.getElementById('yearVal');
            $(calendarEl1).empty()[0].innerHTML = "<span>"+currentDate.substr(0,4)+"&nbsp;&nbsp;년</span><strong>"+currentDate.substr(4,2)+"</strong>월</p>";

            schdMngConstPlanDtl( currentDate, getlastDay(currentDate) );
        });
       
        //다음 버튼
        $('button[id=postBtn]').on('click', function(){
            /*한달후 날짜 구하기*/
            currentDate = getPostMonth(currentDate.substr(0,4),currentDate.substr(4,2),currentDate.substr(6,2));
            var calendarEl1 = document.getElementById('yearVal');
            $(calendarEl1).empty()[0].innerHTML = "<span>"+currentDate.substr(0,4)+"&nbsp;&nbsp;년</span><strong>"+currentDate.substr(4,2)+"</strong>월</p>";

            schdMngConstPlanDtl( currentDate, getlastDay(currentDate) );
        });
       
    /*한달전 날짜 구하기*/
    function getPreMonth(yyyy,mm,dd) {
        var now = new Date(yyyy,(mm-1),dd);
        var preDay = new Date( now.setMonth( now.getMonth() -1 ) );
        return getTodayYMD(preDay.getFullYear(),preDay.getMonth()+1,'01');
    }

    /*한달후 날짜 구하기*/
    function getPostMonth(yyyy,mm,dd) {
        var now = new Date(yyyy,(mm-1),dd);
        var postDay = new Date( now.setMonth( now.getMonth() +1 ) );
        return getTodayYMD(postDay.getFullYear(),postDay.getMonth()+1,'01');
    }
    /* 해당달의 마지막 날짜 구하기 */
    function getlastDay(source) {
        source = source.replace(/\D/ig, "");
        if (isNaN(source) || source.length < 6) {
            return "";
        }
        var year  = source.substring(0, 4);
        var month = source.substring(4, 6);
        var newDate  = new Date(year, month, 0);

        var newYear  = newDate.getFullYear();
        var newMonth = newDate.getMonth()+1;
        var newDay   = newDate.getDate();
        console.log(newMonth);

        if (newMonth.toString().length < 2) newMonth = "0" + newMonth;
        if (newDay.toString().length < 2) newDay = "0" + newDay;

        return newYear.toString() + newMonth.toString() + newDay.toString();
       
    }

 

참조 :  [JavaScript] 한달 전/후 날짜 구하기 (tistory.com)

 

[JavaScript] 한달 전/후 날짜 구하기

Intro JavaScript 에서 현재 시간을 기준으로 한달 전/후 날짜를 구하는 방법에 대해 알아보도록 하겠습니다. How to solve the problem let now = new Date();// 현재 날짜 및 시간 console.log("현재 : ", now); // 현재 :

steady-learner.tistory.com

 

:
Posted by 상문이