IT/development

[JavaScript] ๋‚ ์งœ ์‚ฌ์ด ์ผ์ˆ˜ ๊ณ„์‚ฐ

์•Œ ์ˆ˜ ์—†๋Š” ์‚ฌ์šฉ์ž 2023. 4. 29.
//์‹œ์ž‘์ผ, ์ข…๋ฃŒ์ผ ์‚ฌ์ด ์ผ์ˆ˜ ๊ณ„์‚ฐ(yyyymmdd ํ˜•์‹)
function calDiffDays(startDate, endDate) {
    //์‹œ์ž‘์ผ
    const strDt = new Date(startDate.substring(0,4), startDate.substring(4,6), startDate.substring(6,8));
    //์ข…๋ฃŒ์ผ
    const endDt = new Date(endDate.substring(0,4), endDate.substring(4,6), endDate.substring(6,8));
    //์ผ์ˆ˜ ์ฐจ์ด(์ ˆ๋Œ€๊ฐ’)
    let diffDays = Math.abs(endDt.getTime() - strDt.getTime());
    //ํ•˜๋ฃจ(๋ฐ€๋ฆฌ์„ธ์ปจ๋“œ)๋กœ ๋‚˜๋ˆˆ ๋’ค ๋ฐ˜์˜ฌ๋ฆผ
    return Math.floor(diffDays / (1000 * 60 * 60 * 24));
    
}

์ถœ์ฒ˜: https://www.appletong.com/entry/javascript-%EB%91%90-%EB%82%A0%EC%A7%9C%EA%B0%84%EC%9D%98-%EC%B0%A8%EC%9D%B4-%EA%B5%AC%ED%95%98%EA%B8%B0-yyyyMMdd-%ED%98%95%EC%8B%9D

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ ์งœ๊ฐ„์˜ ์ฐจ์ด ๊ตฌํ•˜๊ธฐ ( yyyyMMdd ํ˜•์‹ )

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ๋‚ ์งœ๊ฐ„์˜ ์ฐจ์ด ๊ตฌํ•˜๊ธฐ ( yyyyMMdd ํ˜•์‹ ) javascript์—์„œ ๋‘๋‚ ์งœ๊ฐ„์˜ ์ฐจ์ด ์ผ์ˆ˜๋ฅผ ๊ตฌํ•ด์•ผํ•  ์ผ์ด ์žˆ์„๋•Œ ์‚ฌ์šฉํ•˜๋ฉด ๋œ๋‹ค. function betweenDay(firstDate, secondDate) { var firstDateObj = new Date(firstDate.sub

www.appletong.com

๋Œ“๊ธ€