javascript - Birthday Date Auto generate year for 18 years old - Stack Overflow

My question is how to automatically set the date range for 18 years old, like for example today is 2015

My question is how to automatically set the date range for 18 years old, like for example today is 2015, so the date range must be 1935-1997, don't mind the 1935. Because the date range is for 18 years old and above, so when it e to 2016 the date range will automatically set to 1935-1998 then so on so forth as the year es by.

Here's the current code with javascript on it

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Datepicker - Select a Date Range</title>
  <link rel="stylesheet" href="//code.jquery/ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery/jquery-1.10.2.js"></script>
  <script src="//code.jquery/ui/1.11.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
    $( "#from2" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
	  changeYear: true,
      numberOfMonths: 1,
      yearRange: '1935:1997',
      onClose: function( selectedDate ) {
        $( "#to2" ).datepicker( "option", "minDate", selectedDate );
      }
    });
    $( "#to2" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
	  changeYear: true,
      numberOfMonths: 1,
      yearRange: '1935:1997' ,
      onClose: function( selectedDate ) {
        $( "#from2" ).datepicker( "option", "maxDate", selectedDate );
      }
    });
  });
  </script>
</head>
<body>
 
<label for="birthday">Birthday</label>
<input type="text" id="from2" name="from">

 
 
</body>
</html>

My question is how to automatically set the date range for 18 years old, like for example today is 2015, so the date range must be 1935-1997, don't mind the 1935. Because the date range is for 18 years old and above, so when it e to 2016 the date range will automatically set to 1935-1998 then so on so forth as the year es by.

Here's the current code with javascript on it

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Datepicker - Select a Date Range</title>
  <link rel="stylesheet" href="//code.jquery./ui/1.11.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery./jquery-1.10.2.js"></script>
  <script src="//code.jquery./ui/1.11.4/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css">
  <script>
  $(function() {
    $( "#from2" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
	  changeYear: true,
      numberOfMonths: 1,
      yearRange: '1935:1997',
      onClose: function( selectedDate ) {
        $( "#to2" ).datepicker( "option", "minDate", selectedDate );
      }
    });
    $( "#to2" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
	  changeYear: true,
      numberOfMonths: 1,
      yearRange: '1935:1997' ,
      onClose: function( selectedDate ) {
        $( "#from2" ).datepicker( "option", "maxDate", selectedDate );
      }
    });
  });
  </script>
</head>
<body>
 
<label for="birthday">Birthday</label>
<input type="text" id="from2" name="from">

 
 
</body>
</html>

Well as you can see you in my javascript function the range function is hardcoded. :(

Share Improve this question asked Oct 24, 2015 at 10:53 VistaVista 2012 gold badges5 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

Get the range:

var startDate = "1935";
var endDate = new Date().getFullYear() - 18;    
var interval = startDate + ":" + endDate;

Set the range to the DatePicker:

yearRange: interval

Fiddle

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745523233a4631356.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信