2
votes

I'm using eonasdan datetimepicker. I chose default date via using options. I opened the page using chrome, firefox 47.0, and microsoft edje 25.1.

  • Firefox Case: The date appears in the textfield. But, if I go to the address bar and hit 'enter', the date shown in the textfield disappears.
  • Chrome and edje Case: When the page opens, the date is not shown in the textfield.

I created a fiddle for this question fiddle, what happens in the fiddle is the same as what happens in the google chrome and internet explorer case.

I tried using the 'viewDate: true' option for datetimepicker which fixed the problem. But it created another problem -- the calendar is not shown when I click the calendar icon.

I have no idea why this is happening. Any help is greatly appreciated.

The same code in the fiddle is below:

<!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 charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
        <title>
                Example
        </title>
        <link rel="stylesheet" type="text/css" media="screen" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css" />
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css"/>
        <link href="//cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/build/css/bootstrap-datetimepicker.css" rel="stylesheet"/>

        <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
        <!--[if lt IE 9]>
            <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
            <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
        <![endif]-->
        <script type="text/javascript" src="//code.jquery.com/jquery-2.1.1.min.js"></script>
        <script type="text/javascript" src="//maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
        <script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>
        <script src="//cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/e8bddc60e73c1ec2475f827be36e1957af72e2ea/src/js/bootstrap-datetimepicker.js"></script>  
    </head>
    <body>  
        <div class="container"> 
            <form id="j_idt11" name="j_idt11" method="post" action="/myPage.xhtml" enctype="application/x-www-form-urlencoded">
                <input type="hidden" name="j_idt11" value="j_idt11" />          
                <div class="row">
                    <div class="col-md-4 col-md-offset-4">          
                        <div class="panel panel-primary">
                            <div class="panel-heading text-center">
                                My Panel                
                            </div>                  
                            <div class="panel-body">
                                <div class="form-group text-right">                                                                                     
                                    <label class="text-right">My Date</label>           
                                    <div class="form-group">                                                                                                 
                                        <div class='input-group date' id='d0'>
                                            <input type="text" name="j_idt11:j_idt15:0:j_idt21" value="2016-09-03" class="form-control text-right" onkeydown="return false" />
                                            <span class="input-group-addon">
                                                <span class="glyphicon glyphicon-time"></span>
                                            </span>
                                        </div>
                                        <script type="text/javascript">
                                            $(function () {                     
                                                var cID = "#";
                                                cID = cID.concat('d0');

                                                var t = '2016-09-03'

                                                var options = {
                                                        format: 'L',                                
                                                        defaultDate: moment(t)
                                                    };                      

                                                $(cID).datetimepicker(options);                     
                                            });
                                        </script>                
                                    </div>           
                                </div>
                            </div>                                                                  
                        </div>
                    </div>                  
                </div>                          
            </form> 
        </div>
    </body>
</html>
2

2 Answers

1
votes

You don't need value attribute for datetimepicker input in your HTML. If you remove it, your code will work as showed in the following example:

var cID = "#";
cID = cID.concat('d0');
var t = '2016-09-03';
var options = {
  format: 'L',
  defaultDate: moment(t)
};                      
$(cID).datetimepicker(options);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment-with-locales.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/js/bootstrap-datetimepicker.min.js"></script>

<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datetimepicker/4.17.37/css/bootstrap-datetimepicker.min.css" rel="stylesheet" />


<div class="container"> 
  <form id="j_idt11" name="j_idt11" method="post" action="/myPage.xhtml" enctype="application/x-www-form-urlencoded">
    <input type="hidden" name="j_idt11" value="j_idt11" />          
    <div class="row">
      <div class="col-md-4 col-md-offset-4">          
        <div class="panel panel-primary">
          <div class="panel-heading text-center">
            My Panel                
          </div>                  
          <div class="panel-body">
            <div class="form-group text-right">
              <label class="text-right">My Date</label>
              <div class="form-group">
                <div class='input-group date' id='d0'>
                  <input type="text" name="j_idt11:j_idt15:0:j_idt21" class="form-control text-right" onkeydown="return false" />
                  <span class="input-group-addon">
                    <span class="glyphicon glyphicon-time"></span>
                  </span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </form>
</div>

If you want to use data attributes you can use data-date-* to set an option or data-date-options setting an option object. In your case you can use data-date-default-date instead of value.

You can find more about data-* initialization here and here.

0
votes

CDN :

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment-with-locales.js"></script>

<script type="text/javascript" src="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/a549aa8780dbda16f6cff545aeabc3d71073911e/src/js/bootstrap-datetimepicker.js"></script> 

<script type="text/javascript" src="https://cdn.rawgit.com/Eonasdan/bootstrap-datetimepicker/a549aa8780dbda16f6cff545aeabc3d71073911e/build/css/bootstrap-datetimepicker.css"></script>

HTML :

<div class="row">
    <div class="col-md-12">
         <h6>datetimepicker1</h6>

        <div class="form-group">
            <div class="input-group date" id="datetimepicker1">
                <input type="text" class="form-control" />  <span class="input-group-addon"><span class="glyphicon-calendar glyphicon"></span></span>
            </div>
        </div>
         <h6>datetimepicker2</h6>
        <input type="text" class="form-control" id="datetimepicker2" />
    </div>
</div>

JavaScript :

$('#datetimepicker1').datetimepicker();
$('#datetimepicker2').datetimepicker();

Use This Code..

This will useful for you..