admin管理员组

文章数量:1429082

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

本文标签: javascriptBirthday Date Auto generate year for 18 years oldStack Overflow