0

I have the following in my MVC View:

    @Html.TextBoxFor(model => model.FromDateCollected, new { style = "width:90px;" }) 

In query I have the following:

    $("#FromDateCollected").datepicker();

To get the selected date, I tried the following:

     var dt = $("#FromDateCollected").datepicker("getDate")

     alert(dt);

but getting:

    [object Object]  

2 Answers 2

1

just get the value of text box

var dt = $("#FromDateCollected").val();
alert(dt);

If you want to get the selected date, when user select a date in the calendar, you can do that on the onSelect event

 $(function() {

   $('#FromDateCollected').datepicker({
      onSelect: function(dateText, inst) {
           alert(dateText);
      }
   });

});

Working sample http://jsfiddle.net/pDmjm/5/

EDIT : as per the comment,

$("#FromDateCollected").datepicker("getDate") will give you a Date object and $("#FromDateCollected").val(); will give you a string with current value in the textbox. write the result to a console and you will see the result.

Sample : http://jsfiddle.net/pDmjm/14/ (check firebug console after alerts)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Question, I tried what you had above and it it shows the date just fine. Is dt a string or is it a date type? I tried the following to convert it to a date: var dt = Date.Parse($('#FromDateCollected').val()) but the alert doesn't even come up with I do alert(dt). Why is that?
0

You can get the date from the textbox value.

var dt = new Date(Date.Parse($('#FromDateCollected').val()));
alert(dt);

2 Comments

Thanks. This does not work for me though. When I do alert(dt) the alert doesn't even come up. If I do it with the Date.Parse it works. Any idea?
I've update the code, Date.Parse parses a date string and returns the number of milliseconds between the date string and midnight of January 1, 1970. You can create a date object from that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.