獲取某一年某一月有多少天
阿新 • • 發佈:2019-01-05
方法一:獲取當前月份有多少天
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文件</title> <script type="text/javascript"> function getCurDays() { var curDate = new Date(); curDate.setMonth(curDate.getMonth() + 1); curDate.setDate(0); alert(curDate.getDate()); } </script> </head> <body onload="getCurDays()"> </body> </html>
方法二:遍歷某一年的每一個月有多少天
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文件</title> <script type="text/javascript"> function init(year){ var str = ""; for(var m = 1 ; m <= 12; m ++) { var dt = new Date(); dt.setFullYear(year); dt.setMonth(m-1); for(var d = 1; d<=32; d++) { dt.setDate(d) if(dt.getDate() != d) { str += year+"年:"+m+"月有"+(d-1)+"天</br>" break; } } } document.write(str) } </script> </head> <body onload="init(1990)"> </body> </html>
方法三:判斷某一年某一月有多少天
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文件</title> <script type="text/javascript"> function getCountDays(year,month) { var date = new Date(); date.setFullYear(year,month); date.setDate(0); alert(date.getDate()); } </script> </head> <body onload="getCountDays(1992,2)"> </body> </html>