標籤:label ref jquery cli smis 選中 led type 事件
第一步:做個 文字框用於顯示年月日的
第二步:點擊文字框觸發bootstrap模態框並顯示
第三步:我是建一個外部js頁面寫的
js中獲得目前時間是年份和月份,形如:201208
//擷取完整的日期
var date=new Date;
var year=date.getFullYear();
var month=date.getMonth()+1;
month =(month<10 ? "0"+month:month);
var mydate = (year.toString()+month.toString());
注意,year.toString()+month.toString()不能寫成year+month。不然如果月份大於等於10,則月份為數字,會和年份相加,如201210,則會變為2022,需要加.toString()
以下是搜到的有用內容:
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( ); //擷取日期與時間
<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>
PHP頁面
<!doctype html><html lang="en"><head><meta charset="UTF-8" /><title>Document</title><script src="jquery-1.11.2.min.js" ></script><script src="bootstrap.min.js" ></script><script src="riqi.js"></script><link href="bootstrap.min.css" rel="stylesheet" type="text/css" /></head><body><input type="text" id="riqi" style="margin-top: 20px; margin-left: 100px;" /><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">×</button> <h4 class="modal-title" id="myModalLabel">日期選擇</h4> </div> <div class="modal-body"> <!-- [email protected] 時間:2017-01-09 描述:裡面放內容,點擊確定顯示 --> <select id="nian"><!--年--> </select> <select id="yue"><!--月--> </select> <select id="tian"><!--天--> </select> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">關閉</button> <button type="button" class="btn btn-primary" id="sure">確定</button><!--把我要選的內容扔到文字框裡--> </div> </div><!-- /.modal-content --> </div><!-- /.modal --></div></body><script type="text/javascript">//這是載入用的$("#riqi").click(function(){$(‘#myModal‘).modal(‘show‘);/*當點擊文字框的時候,要觸發並顯示模態框*/LoadNian();//調出的當前年份LoadYue();//調出的當前月份LoadTian();//調出的當前天})//給年月加個事件。這是操作用的$("#sure").click(function(){var nian = $("#nian").val();//取到後吧這三個值扔到文字框裡面var yue = $("#yue").val();var tian = $("#tian").val();var str = nian+"-"+yue+"-"+tian;//把年月日拼字串$("#riqi").val(str);$(‘#myModal‘).modal(‘hide‘)//選完直接關閉模態框})</script></html>
js頁面下面
// JavaScript Document//給年月加個事件要放上面$(document).ready(function(e) {//當年的選中項變的時候要從新選擇下天數 $("#nian").change(function(){LoadTian();})$("#yue").change(function(){//當月份的下拉變化的時候也要從新載入下天數LoadTian();})});//載入年份function LoadNian(){var date=new Date; var year=date.getFullYear(); //擷取當前年份var str = "";for(var i=year-5;i<year+6;i++)//從當前年份減5,當前年份加6、取前5年後5年//i就等於年份{if(i==year)//預設定位當前年份{str +="<option selected=‘selected‘ value=‘"+i+"‘>"+i+"</option>";//預設定位當前年份}else{str +="<option value=‘"+i+"‘>"+i+"</option>";}}$("#nian").html(str);//找到ID等於nian的下拉把option扔裡面,option等於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 +="<option selected=‘selected‘ value=‘"+i+"‘>"+i+"</option>";}else{str +="<option value=‘"+i+"‘>"+i+"</option>";}}$("#yue").html(str);}//載入天function LoadTian(){var date=new Date;var tian = date.getDate();//擷取當前天var zs = 31; //總天數var nian = $("#nian").val();//取當前選中的年份var yue = $("#yue").val();//取當前選中的月份if(yue == 4 || yue==6 || yue==9 || yue==11)//判斷什麼情況下不等於31,有2個條件一個是年一個是月||或者當月份等於4,6,9,11等於30天{zs = 30;}else if(yue==2)//如果是2月{if((nian%4==0 && nian%100 !=0) || nian%400==0)//普通年條件能被4整除並且不能被100整除。或者能被400整除就是潤年{zs = 29;}else{zs = 28;}}var str = "";for(var i=1;i<zs+1;i++){if(i==tian){str +="<option selected=‘selected‘ value=‘"+i+"‘>"+i+"</option>";}else{str +="<option value=‘"+i+"‘>"+i+"</option>";}}$("#tian").html(str);}
jquery做個日期選擇適用於手機端