使用datepicker中的日期填充标签

时间:2014-07-21 18:48:44

标签: jquery

我正在尝试使用jquery日期选择器中选择的日期来测试填充标签。我在标签上得到的是[OBJECT] [OBJECT]。有任何想法吗?

以下是代码:

 @using (Html.BeginForm("Calculate Days", "Calendar", FormMethod.Post))
    {
        <h2>Enter The Number Of Days To Display</h2>

        @Html.TextBoxFor(model => model.DaysToBeViewed)

        <label id="dateSelected"></label>
        <input type="submit" value="Submit" onclick="daysAdded()" />
    }

    <script type="text/javascript">


        var dateSelected = $("#date").datepicker("getDate");

        function daysAdded(addDaysEntered, dateSelected) {

            var oneDAY = 1000 * 60 * 60 * 24
            var addDaysEnteredMiliSec = addDaysEntered.getTime()
            var DateMiliSec = Date.getTime()
            var datesAddedmiliSec = Math.abs(addDaysEnteredMiliSec + DateMiliSec)
            return Math.round(datesAddedmiliSec / oneDay)
        }
     </script>

     <script>
        document.getElementById('dateSelected').innerHTML = dateSelected;
     </script>

    <script>

        $(function () {
            $('#Date').datepicker();

        });

    </script>

1 个答案:

答案 0 :(得分:0)

http://api.jqueryui.com/datepicker/#method-getDate所述,getDate()方法返回datepicker的当前日期,如果没有选择日期,则返回null。返回的对象类型是Date对象,而不是字符串。

The Date type is a JavaScript object that represents a single moment in time. Date objects are instantiated using their constructor function, which by default creates an object that represents the current date and time.

您需要将Date对象转换为字符串。例如:

var dateSelected = $("#date").datepicker("getDate").toString();

http://www.w3schools.com/jsref/jsref_obj_date.asp涵盖了可用的日期对象方法。