jquery作個日期選擇適用於手機端

第一步:作個 文本框用於顯示年月日的css

第二步:點擊文本框觸發 
 var month=date.getMonth()+1;
 month =(month<10 ? "0"+month:month); 
 var mydate = (year.toString()+month.toString());
html

注意,year.toString()+month.toString()不能寫成year+month。否則若是月份大於等於10,則月份爲數字,會和年份相加,如201210,則會變爲2022,須要加.toString()jquery


 

如下是搜到的有用內容:bootstrap

var myDate = new Date();
myDate.getYear(); //獲取當前年份(2位)
myDate.getFullYear(); //獲取完整的年份(4位,1970-????)
myDate.getMonth(); //獲取當前月份(0-11,0表明1月)
myDate.getDate(); //獲取當前日(1-31)
myDate.getDay(); //獲取當前星期X(0-6,0表明星期天)
myDate.getTime(); //獲取當前時間(從1970.1.1開始的毫秒數)
myDate.getHours(); //獲取當前小時數(0-23)
myDate.getMinutes(); //獲取當前分鐘數(0-59)
myDate.getSeconds(); //獲取當前秒數(0-59)
myDate.getMilliseconds(); //獲取當前毫秒數(0-999)
myDate.toLocaleDateString(); //獲取當前日期
var mytime=myDate.toLocaleTimeString(); //獲取當前時間
myDate.toLocaleString( ); //獲取日期與時間
xss

 

 

<SCRIPT LANGUAGE="JavaScript">
function monthnow(){
 var now   = new Date();
 var monthn = now.getMonth();
 var yearn  = now.getYear();
 window.location.href="winnNamelist.jsp?getMonth="+monthn+"&getYear="+yearn;
}
</script>jsp

<!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 src="../jquery/jquery-1.11.2.min.js"></script>
<script src="../../bootstrap/dist/js/bootstrap.min.js"></script>
<script src="riqi.js"></script>
<link href="../../bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
</head>

<body>

<!-- 模態框(Modal) -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel">請選擇日期</h4>
            </div>
            <div class="modal-body">
                <select id="nian">
                </select>
                <select id="yue">
                </select>
                <select id="tian">
                </select>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary" id="qd">肯定</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal -->
</div>

<input type="text" id="rq" />

</body>
</html>
// JavaScript Document
$(document).ready(function(e) {
    //在文本框裏面顯示當前日期
    var date = new Date();
    var nian = date.getFullYear();
    var yue = date.getMonth()+1;
    var tian = date.getDate();
    
    $("#rq").val(nian+"-"+yue+"-"+tian);
    
    //給文本框加點擊事件
    $("#rq").click(function(){
            $('#myModal').modal('show');
            //加載年
            LoadNian();
            //加載月
            LoadYue();
            //加載天
            LoadTian();
        })
        
    //當年份選中變化的時候,從新加載天
    $("#nian").change(function(){
            LoadTian();
        })
    //當月份選中變化的時候,從新加載天
    $("#yue").change(function(){
            LoadTian();
        })
    //給肯定按鈕加點擊事件
    $("#qd").click(function(){
            var nian = $("#nian").val();
            var yue = $("#yue").val();
            var tian = $("#tian").val();
            
            $("#rq").val(nian+"-"+yue+"-"+tian);
            $('#myModal').modal('hide');
        })
    
});

//加載年的方法
function LoadNian()
{
    var date = new Date();
    var nian = date.getFullYear();
    
    var str = "";
    for(var i=nian-5;i<nian+6;i++)
    {
        if(i==nian)
        {
            str = str +"<option selected='selected' value='"+i+"'>"+i+"</option>";
        }
        else
        {
            str = str +"<option value='"+i+"'>"+i+"</option>";
        }
    }
    $("#nian").html(str);
}

//加載月
function LoadYue()
{
    var date = new Date();
    var yue = date.getMonth()+1;
    
    var str = "";
    for(var i=1;i<13;i++)
    {
        if(i==yue)
        {
            str = str+"<option selected='selected' value='"+i+"'>"+i+"</option>";
        }
        else
        {
            str = str+"<option value='"+i+"'>"+i+"</option>";
        }
    }
    $("#yue").html(str);
}

//加載天
function LoadTian()
{
    var date = new Date();
    var tian = date.getDate();
    var yue = $("#yue").val();
    var nian = $("#nian").val();
    
    var zs = 31;
    if(yue==4 || yue==6 || yue==9 || yue==11)
    {
        zs = 30;
    }
    else if(yue==2)
    {
        if((nian%4==0 && nian%100!=0) || nian%400==0 )
        {
            zs = 29;
        }
        else
        {
            zs = 28;
        }
    }
    
    var str = "";
    for(var i=1;i<zs+1;i++)
    {
        if(i==tian)
        {
            str = str +"<option selected='selected' value='"+i+"'>"+i+"</option>";
        }
        else
        {
            str = str +"<option value='"+i+"'>"+i+"</option>";
        }
    }
    
    $("#tian").html(str);
    
}

 

 

 

相關文章
相關標籤/搜索