JavaScript / Object / Date / 당월 말일 구하는 방법
다음은 2022년 3월 1일이다.
Date( 2022, 2, 1 )
날짜 자리에 0이 들어가면 전달 말일이다. 즉 다음은 2022년 2월 28일이다.
Date( 2022, 2, 0 )
이를 이용하여 현재 달의 말일을 구할 수 있다. 다음은 오늘 날짜를 출력하는 것이고...
<!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>JavaScript</title> <style> body { font-family: Consolas, monospace; font-size: 20px; } </style> </head> <body> <script> var today = new Date(); document.write( '<p>' + today.getFullYear() + '-' + ( today.getMonth() + 1 ) + '-' + today.getDate() + '</p>' ); </script> </body> </html>
다음은 현재 날짜 뒤에 당월 말일을 출력하는 것이다.
<!doctype html> <html lang="ko"> <head> <meta charset="utf-8"> <title>JavaScript</title> <style> body { font-family: Consolas, monospace; font-size: 20px; } </style> </head> <body> <script> var today = new Date(); document.write( '<p>' + today.getFullYear() + '-' + ( today.getMonth() + 1 ) + '-' + today.getDate() + '</p>' ); var lastDay = new Date( today.getFullYear(), ( today.getMonth() + 1 ), 0 ); document.write( '<p>' + today.getFullYear() + '-' + ( today.getMonth() + 1 ) + '-' + lastDay.getDate() + '</p>' ); </script> </body> </html>