js根據(jù)給定的日期計算當(dāng)月有多少天實(shí)現(xiàn)思路及代碼
更新時間:2013年02月25日 11:02:13 作者:
根據(jù)給定的日期計算當(dāng)月有多少天,想必這樣的功能大家都想實(shí)現(xiàn)吧,所以本文的出現(xiàn)相當(dāng)有必要,接下來看下實(shí)現(xiàn)代碼,感興趣的朋友可以了解下,希望對你有所幫助
這個代碼沒什么,貼上來,我想表達(dá)的重點(diǎn)隱藏在代碼中,找找看!
<!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>
<title>show date</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
name="mark";
function showLastDate()
{
alert(name);
var reg=/^\d{4}\/\d{1,2}\/\d{1,2}$/;
var inputdate=$("#indate").val();
if(!reg.test(inputdate))
{
alert("please input date like:2013/1/14");
return;
}
var month=parseInt(inputdate.split("/")[1]);
if(month>12||month==0)
{
alert("please input month range from 1-12");
return;
}
var showdate=getLastDate(inputdate);
$("#lastdate").val(showdate);
}
function getLastDate(indate)
{
alert(name);
var year=parseInt(indate.split("/")[0]);
var month=parseInt(indate.split("/")[1]);
//run nian
var isrun=false;
if((year%4==0 && year%100!=0)|| year%400==0)
isrun=true;
switch(month)
{
case 2:
if(isrun)
{return 29;}
else
{return 28;}
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
default:
return 30;
}
}
</script>
</head>
<body style="margin-left:100px;margin-top:20px;">
<br><br>
input date:<input id="indate" type="text" />example:2013/1/14<br><br>
<input type="button" onclick="showLastDate()" value="Get Last Date"/><br><br>
input date:<input id="lastdate" type="text" /><br>
</body>
</html>
復(fù)制代碼 代碼如下:
<!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>
<title>show date</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.2.6/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
name="mark";
function showLastDate()
{
alert(name);
var reg=/^\d{4}\/\d{1,2}\/\d{1,2}$/;
var inputdate=$("#indate").val();
if(!reg.test(inputdate))
{
alert("please input date like:2013/1/14");
return;
}
var month=parseInt(inputdate.split("/")[1]);
if(month>12||month==0)
{
alert("please input month range from 1-12");
return;
}
var showdate=getLastDate(inputdate);
$("#lastdate").val(showdate);
}
function getLastDate(indate)
{
alert(name);
var year=parseInt(indate.split("/")[0]);
var month=parseInt(indate.split("/")[1]);
//run nian
var isrun=false;
if((year%4==0 && year%100!=0)|| year%400==0)
isrun=true;
switch(month)
{
case 2:
if(isrun)
{return 29;}
else
{return 28;}
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
default:
return 30;
}
}
</script>
</head>
<body style="margin-left:100px;margin-top:20px;">
<br><br>
input date:<input id="indate" type="text" />example:2013/1/14<br><br>
<input type="button" onclick="showLastDate()" value="Get Last Date"/><br><br>
input date:<input id="lastdate" type="text" /><br>
</body>
</html>
相關(guān)文章
使用snowfall.jquery.js實(shí)現(xiàn)愛心滿屏飛的效果
這篇文章主要介紹了使用snowfall.jquery.js實(shí)現(xiàn)愛心滿屏飛的效果的相關(guān)資料,需要的朋友可以參考下2017-01-01JS+ACTIVEX實(shí)現(xiàn)網(wǎng)頁選擇本地目錄路徑對話框
使用ACTIVEX對象遍歷本地磁盤和目錄,只需網(wǎng)頁啟用相關(guān)ACTIVEX的INTERNET選項(xiàng)即可實(shí)現(xiàn)路徑選擇對話框,感興趣的你可以參考下哈希望可以幫助到你2013-03-03webpack踩坑系列之less-loader6.0.0的javascriptEnabled報錯問題
這篇文章主要介紹了webpack踩坑系列之less-loader6.0.0的javascriptEnabled報錯問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01JavaScript 對象深入學(xué)習(xí)總結(jié)(經(jīng)典)
JavaScript中,除了五種原始類型(即數(shù)字,字符串,布爾值,null,undefined)之外的都是對象了,所以,不把對象學(xué)明白怎么繼續(xù)往下學(xué)習(xí)呢?本篇文章給大家分享javascript對象深入學(xué)習(xí)總結(jié),小伙伴們跟著小編一起深入學(xué)習(xí)吧2015-09-09簡單幾行JS Code實(shí)現(xiàn)IE郵件轉(zhuǎn)發(fā)新浪微博
大概就是說我們可以用window.external.menuArguments這個對象獲取到內(nèi)部的信息,如window,document這些常用的對象2013-07-07原生JS實(shí)現(xiàn)動態(tài)添加新元素、刪除元素方法
這篇文章主要介紹了原生js實(shí)現(xiàn)動態(tài)添加新元素、刪除元素方法 ,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2019-05-05