Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • To show the current date month and year using javascript

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 126
    Comment on it

    Hi,

     

    Below code example will help you to "show the current date month and year using javascript".

     

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 
    Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Conforming XHTML 1.0 Transitional Template</title>
    <script type="text/javascript">
    
    window.onload = init;
    function init() {
          // varible used for storing date 
    	var d = new Date();
    	var val =document.months.DOBYear.options;
    	alert(new Date().getFullYear());
    // function used to get month
    	document.months.mStr.options[new Date().getMonth()].selected = true;
    // function used to get day
        document.months.DOBDay.options[new Date().getDate()].selected = true;
    // function used to get year
        for(i=1;i<document.months.DOBYear.options.length;i++) {
          if (document.months.DOBYear.options[i].value == d.getFullYear()) {
             document.months.DOBYear.options[i].selected = true;
             break;
          }
       }
        
    }
    
    
    </script>
    </head>
    <body>
    
    <form name="months" action="">
    // values for months
    <select name="mStr">
    	<option value="0">January</option>
    	<option value="1">February</option>
    	<option value="2">March</option>
    	<option value="3">April</option>
    	<option value="4">May</option>
    	<option value="5">June</option>
    	<option value="6">July</option>
    	<option value="7">August</option>
    	<option value="8">September</option>
    	<option value="9">October</option>
    	<option value="10">November</option>
    	<option value="11">December</option>
    </select>
    
    // values for days
        <select name="DOBDay">
          <option value="0">1</option>
          <option value="1">2</option>
          <option value="2">3</option>
          <option value="3">4</option>
          <option value="4">5</option>
          <option value="5">6</option>
          <option value="6">7</option>
          <option value="7">8</option>
          <option value="8">9</option>
          <option value="9">10</option>
          <option value="10">11</option>
          <option value="11">12</option>
          <option value="12">13</option>
          <option value="13">14</option>
          <option value="14">15</option>
          <option value="15">16</option>
          <option value="16">17</option>
          <option value="17">18</option>
          <option value="18">19</option>
          <option value="19">20</option>
          <option value="20">21</option>
          <option value="21">22</option>
          <option value="22">23</option>
          <option value="23">24</option>
          <option value="24">25</option>
          <option value="25">26</option>
          <option value="26">27</option>
          <option value="27">28</option>
          <option value="28">29</option>
          <option value="29 ">30</option>
          <option value="30">31</option>
        </select>
    // values for year
        <select name="DOBYear">
          <option value="0">2009</option>
          <option value="1">2010</option>
          <option value="2">2011</option>
          <option value="3">2012</option>
          <option value="4">2013</option>
          <option value="5">2014</option>
          <option value="6">2015</option>
          <option value="7">2016</option>
          <option value="8">2017</option>
        </select>
    
    </form>
    </body>
    </html>

    Here is the code will show the dropdown for date month and year. When the window is loaded it shows the current date month and year in the dropdown.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: