开发者

jquery ui datepicker getDate returns Object instead of Date

开发者 https://www.devze.com 2023-04-11 21:29 出处:网络
I\'m probably missing something simple here, but I\'m having trouble with the jquery ui datepicker. It seems like it is just returning a generic Object instead of a Date object, which is not what I ex

I'm probably missing something simple here, but I'm having trouble with the jquery ui datepicker. It seems like it is just returning a generic Object instead of a Date object, which is not what I expected. Here is the code:

<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">

  <link type="text/css" href="jquery_ui/css/custom-theme/jquery-ui-1.8.14.custom.css" rel="stylesheet" />    
  <script type="text/javascript" src="jquery_ui/js/jquery-1.6.2.min.js"></script>
  <script type="text/javascript" src="jquery_ui/js/jquery-ui-1.8.16.custom.min.js"></script>
  <sc开发者_开发问答ript type="text/javascript">

    Date.prototype.addHours= function(h){
        this.setHours(this.getHours()+h);
        return this;
    }

    $(function() {

        $( "#startdatetime" ).datepicker( 
            {
                onSelect: function(dateText, inst) {
                    sd = $( "startdatetime" ).datepicker( 'getDate' );
                    sd.addHours(10);
                }
            }); 

    });
  </script>
</head>

<body >
    <input id="startdatetime" type="text">
</body>
</html>

The exception I get is "sd.addHours is not a function", and using firebug it looks like getDate is giving me the wrong object (not a Date).

I'm not very familiar with javascript or jquery, does anyone know why this is happening?


You're missing the pound sign in the inner jquery expression. You don't actually need to locate the object again in any case, as the instance is passed into the function as inst.


There is method in addHour native JavaScript Date class (see http://www.w3schools.com/jsref/jsref_obj_date.asp).

What you could do is this code below:

Date.prototype.addHours = function(amount) {
return new Date(this.getTime() + (amount * 60 * 60 * 1000));
}

$("#datepicker").datepicker("getDate").addHours(10);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号