JavaScript自動生成 年月范圍 選擇功能完整示例【基于jQuery插件】
本文實例講述了JavaScript實現(xiàn)的自動生成 年月范圍 選擇功能。分享給大家供大家參考,具體如下:
近日做項目涉及到日期選擇,為了用戶界面友好,于是加入了一年內(nèi)的年月段的查詢功能,先看效果
會自動判斷當(dāng)前年份
以下為html代碼 其中用到了 Jquery 和 struts 標(biāo)簽 但是這兩個都不是重要的 主要書 用于賦值 和 取值方便
還用到了 WdatePicker 插件進行具體日期選擇
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script type="text/javascript"> $(function(){ selectMonth(); }) //年月選擇 star function selectMonth(){ var myDate = new Date(); var year = myDate.getUTCFullYear(); var month = myDate.getUTCMonth (); var dateList = new Array(); var endDay; for(var i=0;i<=12;i++){ var m = month+i; endDay = maxDay(m,year-1); if(m<12) dateList.push((year-1)+"-"+(m+1)+"~"+endDay); else dateList.push(year+"-"+(m-11)+"~"+endDay); } dateList.reverse(); $.each(dateList,function(idx,item){ var ym = item.split("~"); var mon = ym[0].split("-"); if(mon[1]==(month+1) && mon[0] == year) $("#dateList").append("<option value="+myDate.getDate()+">"+"本月"+"</option>"); else $("#dateList").append("<option value="+ym[1]+">"+ym[0]+"</option>"); }) getEndTime(); } function maxDay(month,year){//獲得某年某月最大天數(shù) var d= new Date(); d.setUTCFullYear(year,month); return new Date(d.getFullYear(), d.getMonth()+1,0).getDate(); } function getEndTime(){ //動態(tài)生成 月初日期 和 月末日期 var list = $("#dateList option:selected"); var selMonth = $("#dateList option:selected").html() if( selMonth == "本月"){ var d = new Date(); $("#starTime").val(d.getUTCFullYear()+"-"+(d.getUTCMonth()+1)+"-1"); $("#endTime").val(d.getUTCFullYear()+"-"+(d.getUTCMonth()+1)+"-"+list.val()); }else{ $("#starTime").val(selMonth+"-1"); $("#endTime").val(selMonth+"-"+list.val()); } } //年月選擇end </script> <body> <td nowrap="nowrap" style="width: 15%" align="center"> 日期: <select id="dateList" οnchange="getEndTime()"> </select> 從 <input name="starTime" id="starTime" value="<s:date name="starTime" format="yyyy-MM-dd"/>" onFocus="WdatePicker()" class="Wdate" style="width: 110px; height: 17px; border-left: 0; border-top: 0; border-right: 0; border-bottom-color: #C06" /> 至 <input name="endTime" id="endTime" value="<s:date name="endTime" format="yyyy-MM-dd"/>" onFocus="WdatePicker()" class="Wdate" style="width: 110px; height: 17px; border-left: 0; border-top: 0; border-right: 0; border-bottom-color: #C06" /> </td> </body>
以上為核心代碼,有需要的兄弟盡管拿去用,如有更好的請幫忙推薦。
PS:這里再為大家推薦幾款時間及日期相關(guān)工具供大家參考使用:
在線日期/天數(shù)計算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi
在線日期計算器/相差天數(shù)計算器:
http://tools.jb51.net/jisuanqi/datecalc
在線日期天數(shù)差計算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq
Unix時間戳(timestamp)轉(zhuǎn)換工具:
http://tools.jb51.net/code/unixtime
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery日期與時間操作技巧總結(jié)》、《jQuery擴展技巧總結(jié)》、《jQuery常見事件用法與技巧總結(jié)》、《jQuery常用插件及用法總結(jié)》、《jQuery常見經(jīng)典特效匯總》及《jquery選擇器用法總結(jié)》
希望本文所述對大家jQuery程序設(shè)計有所幫助。
- jquery 日期分離成年月日的代碼
- jQuery實現(xiàn)倒計時(倒計時年月日可自己輸入)
- jQuery實現(xiàn)動態(tài)生成年月日級聯(lián)下拉列表示例
- jquery中實現(xiàn)時間戳與日期相互轉(zhuǎn)換
- 獲取客戶端電腦日期時間js代碼(jquery)
- jQuery DateTimePicker 日期和時間插件示例
- jQuery帶時間的日期控件代碼分享
- jQuery移動端日期(datedropper)和時間(timedropper)選擇器附源碼下載
- jquery仿蘋果的時間/日期選擇效果
- jQuery時間日期三級聯(lián)動(推薦)
- 貼近用戶體驗的Jquery日期、時間選擇插件
- jQuery時間戳和日期相互轉(zhuǎn)換操作示例
相關(guān)文章
Jquery 復(fù)選框取值兼容FF和IE8(測試有效)
Jquery 復(fù)選框取值的文章網(wǎng)上有很多的,不過可以同時兼容FF和IE8的確實沒有幾個,下面有個不錯的方法經(jīng)測試有效2013-10-10jQuery實現(xiàn)大轉(zhuǎn)盤抽獎活動仿QQ音樂代碼分享
這篇文章主要展示了jQuery實現(xiàn)大轉(zhuǎn)盤抽獎活動仿QQ音樂實現(xiàn)代碼,需要的朋友可以參考下2015-08-08