js实现日期选择器的两种方法(默认展示为最近一周)

JavaScript020

js实现日期选择器的两种方法(默认展示为最近一周),第1张

方法一:

读取当前日期,再将day-7,判断day<=0针对不同月份的显示情况:

方法二:

先读取当前日期,再读取七天前日期。

显然,第二种方法简便了很多。

一下好一款laydate.js日期时间选择插件,具有一定的参考价值,希望可以帮到你

日期时间选择插件laydate.js:

效果图:

1. 引入JS。 

<script type="text/javascript" src="js/laydate.js"></script>

2. 根据需要做相应的配置。详情参看官网

<script>

 laydate({

  elem: '#seldate', //目标元素。由于laydate.js封装了一个轻量级的选择器引擎,因此elem还允许你传入class、tag但必须按照这种方式 '#id .class'

  event: 'focus', //响应事件。如果没有传入event,则按照默认的click

  format: 'YYYY/MM/DD hh:mm:ss', // 分隔符可以任意定义,该例子表示只显示年月

  festival: true, //显示节日

  istime: true, //显示时间选项

  choose: function(datas){ //选择日期完毕的回调

   alert('得到:'+datas)

  }

 })

</script>

实例源码:

<!DOCTYPE html> 

<html> 

<head> 

 <meta charset="utf-8"> 

 <meta http-equiv="X-UA-Compatible" content="IE=edge"> 

 <meta name="viewport" content="width=device-width, initial-scale=1"> 

 <!-- 

 <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" /> 

 <link href="favicon.ico" rel="Bookmark" type="image/x-icon" /> 

 -->

 <meta name="Generator" content="EditPlus®"> 

 <meta name="Author" content=""> 

 <meta name="Keywords" content=""> 

 <meta name="Description" content=""> 

 <title>layDate日期时间选择插件</title> 

 <link href="" rel="stylesheet" /> 

 <script type="text/javascript" src="js/laydate.js"></script> 

</head> 

<body> 

 <form method="post" action=""> 

 Way1, 

  请选择日期:<input type="text" name="date" onclick="laydate()" /> 

  <hr /> 

 Way2, 

  <input type="text" name="date" id='seldate' class="laydate-icon" /><hr /> 

  <script> 

   laydate({ 

    elem: '#seldate', //目标元素。由于laydate.js封装了一个轻量级的选择器引擎,因此elem还允许你传入class、tag但必须按照这种方式 '#id .class' 

    event: 'focus', //响应事件。如果没有传入event,则按照默认的click 

    format: 'YYYY/MM/DD hh:mm:ss', // 分隔符可以任意定义,该例子表示只显示年月 

    festival: true, //显示节日 

    istime: true, //显示时间选项 

    choose: function(datas){ //选择日期完毕的回调 

     alert('得到:'+datas) 

    } 

   }) 

  </script> 

 Way3, 

  <input id="seldate1"> 

  <span class="laydate-icon" onclick="laydate({elem:'#seldate1'})"></span> 

 </form> 

</body> 

</html>

MessageDigest digest = MessageDigest.getInstance("MD5")

digest.update("dinghua".getBytes())

String pwd = new String(digest.digest())

<%@page pageEncoding="utf-8"%>

<html>

<head>

<title>JS日期选择器</title>

<script type="text/javascript">

function HS_DateAdd(interval,number,date){

number = parseInt(number)

if (typeof(date)=="string"){var date = new Date(date.split("-")[0],date.split("-")[1],date.split("-")[2])}

if (typeof(date)=="object"){var date = date}

switch(interval){

case "y":return new Date(date.getFullYear()+number,date.getMonth(),date.getDate())break

case "m":return new Date(date.getFullYear(),date.getMonth()+number,checkDate(date.getFullYear(),date.getMonth()+number,date.getDate()))break

case "d":return new Date(date.getFullYear(),date.getMonth(),date.getDate()+number)break

case "w":return new Date(date.getFullYear(),date.getMonth(),7*number+date.getDate())break

}

}

function checkDate(year,month,date){

var enddate = ["31","28","31","30","31","30","31","31","30","31","30","31"]

var returnDate = ""

if (year%4==0){enddate[1]="29"}

if (date>enddate[month]){returnDate = enddate[month]}else{returnDate = date}

return returnDate

}

function WeekDay(date){

var theDate

if (typeof(date)=="string"){theDate = new Date(date.split("-")[0],date.split("-")[1],date.split("-")[2])}

if (typeof(date)=="object"){theDate = date}

return theDate.getDay()

}

function HS_calender(){

var lis = ""

var style = ""

style +="<style type='text/css'>"

style +=".calender { width:170pxheight:autofont-size:12pxmargin-right:14pxbackground:url(calenderbg.gif) no-repeat right center #fffborder:1px solid #397EAEpadding:1px}"

style +=".calender ul {list-style-type:nonemargin:0padding:0}"

style +=".calender .day { background-color:#EDF5FFheight:20px}"

style +=".calender .day li,.calender .date li{ float:leftwidth:14%height:20pxline-height:20pxtext-align:center}"

style +=".calender li a { text-decoration:nonefont-family:Tahomafont-size:11pxcolor:#333}"

style +=".calender li a:hover { color:#f30text-decoration:underline}"

style +=".calender li a.hasArticle {font-weight:boldcolor:#f60 !important}"

style +=".lastMonthDate, .nextMonthDate {color:#bbbfont-size:11px}"

style +=".selectThisYear a, .selectThisMonth a{text-decoration:nonemargin:0 2pxcolor:#000font-weight:bold}"

style +=".calender .LastMonth, .calender .NextMonth{ text-decoration:nonecolor:#000font-size:18pxfont-weight:boldline-height:16px}"

style +=".calender .LastMonth { float:left}"

style +=".calender .NextMonth { float:right}"

style +=".calenderBody {clear:both}"

style +=".calenderTitle {text-align:centerheight:20pxline-height:20pxclear:both}"

style +=".today { background-color:#ffffaaborder:1px solid #f60padding:2px}"

style +=".today a { color:#f30}"

style +=".calenderBottom {clear:bothborder-top:1px solid #dddpadding: 3px 0text-align:left}"

style +=".calenderBottom a {text-decoration:nonemargin:2px !importantfont-weight:boldcolor:#000}"

style +=".calenderBottom a.closeCalender{float:right}"

style +=".closeCalenderBox {float:rightborder:1px solid #000background:#ffffont-size:9pxwidth:11pxheight:11pxline-height:11pxtext-align:centeroverflow:hiddenfont-weight:normal !important}"

style +="</style>"

var now

if (typeof(arguments[0])=="string"){

selectDate = arguments[0].split("-")

var year = selectDate[0]

var month = parseInt(selectDate[1])-1+""

var date = selectDate[2]

now = new Date(year,month,date)

}else if (typeof(arguments[0])=="object"){

now = arguments[0]

}

var lastMonthEndDate = HS_DateAdd("d","-1",now.getFullYear()+"-"+now.getMonth()+"-01").getDate()

var lastMonthDate = WeekDay(now.getFullYear()+"-"+now.getMonth()+"-01")

var thisMonthLastDate = HS_DateAdd("d","-1",now.getFullYear()+"-"+(parseInt(now.getMonth())+1).toString()+"-01")

var thisMonthEndDate = thisMonthLastDate.getDate()

var thisMonthEndDay = thisMonthLastDate.getDay()

var todayObj = new Date()

today = todayObj.getFullYear()+"-"+todayObj.getMonth()+"-"+todayObj.getDate()

for (i=0i<lastMonthDatei++){ // Last Month's Date

lis = "<li class='lastMonthDate'>"+lastMonthEndDate+"</li>" + lis

lastMonthEndDate--

}

for (i=1i<=thisMonthEndDatei++){ // Current Month's Date

if(today == now.getFullYear()+"-"+now.getMonth()+"-"+i){

var todayString = now.getFullYear()+"-"+(parseInt(now.getMonth())+1).toString()+"-"+i

lis += "<li><a href=javascript:void(0) class='today' onclick='_selectThisDay(this)' title='"+now.getFullYear()+"-"+(parseInt(now.getMonth())+1)+"-"+i+"'>"+i+"</a></li>"

}else{

lis += "<li><a href=javascript:void(0) onclick='_selectThisDay(this)' title='"+now.getFullYear()+"-"+(parseInt(now.getMonth())+1)+"-"+i+"'>"+i+"</a></li>"

}

}

var j=1

for (i=thisMonthEndDayi<6i++){ // Next Month's Date

lis += "<li class='nextMonthDate'>"+j+"</li>"

j++

}

lis += style

var CalenderTitle = "<a href='javascript:void(0)' class='NextMonth' onclick=HS_calender(HS_DateAdd('m',1,'"+now.getFullYear()+"-"+now.getMonth()+"-"+now.getDate()+"'),this) title='Next Month'>»</a>"

CalenderTitle += "<a href='javascript:void(0)' class='LastMonth' onclick=HS_calender(HS_DateAdd('m',-1,'"+now.getFullYear()+"-"+now.getMonth()+"-"+now.getDate()+"'),this) title='Previous Month'>«</a>"

CalenderTitle += "<span class='selectThisYear'><a href='javascript:void(0)' onclick='CalenderselectYear(this)' title='Click here to select other year' >"+now.getFullYear()+"</a></span>年<span class='selectThisMonth'><a href='javascript:void(0)' onclick='CalenderselectMonth(this)' title='Click here to select other month'>"+(parseInt(now.getMonth())+1).toString()+"</a></span>月"

if (arguments.length>1){

arguments[1].parentNode.parentNode.getElementsByTagName("ul")[1].innerHTML = lis

arguments[1].parentNode.innerHTML = CalenderTitle

}else{

var CalenderBox = style+"<div class='calender'><div class='calenderTitle'>"+CalenderTitle+"</div><div class='calenderBody'><ul class='day'><li>日</li><li>一</li><li>二</li><li>三</li><li>四</li><li>五</li><li>六</li></ul><ul class='date' id='thisMonthDate'>"+lis+"</ul></div><div class='calenderBottom'><a href='javascript:void(0)' class='closeCalender' onclick='closeCalender(this)'>×</a><span><span><a href=javascript:void(0) onclick='_selectThisDay(this)' title='"+todayString+"'>Today</a></span></span></div></div>"

return CalenderBox

}

}

function _selectThisDay(d){

var boxObj = d.parentNode.parentNode.parentNode.parentNode.parentNode

boxObj.targetObj.value = d.title

boxObj.parentNode.removeChild(boxObj)

}

function closeCalender(d){

var boxObj = d.parentNode.parentNode.parentNode

boxObj.parentNode.removeChild(boxObj)

}

function CalenderselectYear(obj){

var opt = ""

var thisYear = obj.innerHTML

for (i=1970i<=2020i++){

if (i==thisYear){

opt += "<option value="+i+" selected>"+i+"</option>"

}else{

opt += "<option value="+i+">"+i+"</option>"

}

}

opt = "<select onblur='selectThisYear(this)' onchange='selectThisYear(this)' style='font-size:11px'>"+opt+"</select>"

obj.parentNode.innerHTML = opt

}

function selectThisYear(obj){

HS_calender(obj.value+"-"+obj.parentNode.parentNode.getElementsByTagName("span")[1].getElementsByTagName("a")[0].innerHTML+"-1",obj.parentNode)

}

function CalenderselectMonth(obj){

var opt = ""

var thisMonth = obj.innerHTML

for (i=1i<=12i++){

if (i==thisMonth){

opt += "<option value="+i+" selected>"+i+"</option>"

}else{

opt += "<option value="+i+">"+i+"</option>"

}

}

opt = "<select onblur='selectThisMonth(this)' onchange='selectThisMonth(this)' style='font-size:11px'>"+opt+"</select>"

obj.parentNode.innerHTML = opt

}

function selectThisMonth(obj){

HS_calender(obj.parentNode.parentNode.getElementsByTagName("span")[0].getElementsByTagName("a")[0].innerHTML+"-"+obj.value+"-1",obj.parentNode)

}

function HS_setDate(inputObj){

var calenderObj = document.createElement("span")

calenderObj.innerHTML = HS_calender(new Date())

calenderObj.style.position = "absolute"

calenderObj.targetObj = inputObj

inputObj.parentNode.insertBefore(calenderObj,inputObj.nextSibling)

}

</script>

<style>

body {font-size:12px}

td {text-align:center}

h1 {font-size:26px}

h4 {font-size:16px}

em {color:#999margin:0 10pxfont-size:11pxdisplay:block}

</style>

</head>

<body>

<h1>Date Picker Demo By Codefans.net</h1>

<h4 style="border-bottom:1px solid #ccc">ver:1.0</h4>

<table border="1" width="400" height="150">

<tr>

<td>这是示例文字</td>

<td>示例输入框</td>

<td>文本文本文本</td>

</tr>

<tr>

<td>示例输入框</td>

<td><input type="text" style="width:70px" onfocus="HS_setDate(this)">文本</td>

<td>这里是你的文字</td>

</tr>

<tr>

<td>一段文字</td>

<td>示例输入框</td>

<td>文本<input type="text" style="width:70px" onfocus="HS_setDate(this)">文本</td>

</tr>

</table>

<ul>

<li>它不需要其他框架类支持</li>

<li>支持多种浏览器</li>

<li>点击年份、月份可下拉选择</li>

</ul>

</body>

</html>