3

I have a datepicker in 2 textboxes , in am trying to add the value of these text boxes to a function that needs a valid Javascript Date.

This is the part of the function - start is a Date

        else if (daystosearch == 'custom') {
            start.setDate(Date.parse($('#<%= tStartDate.ClientID %>').val()));
            end.setDate(Date.parse($('#<%= tEndDate.ClientID %>').val()));
        }

Date.Parse will not work , because value of the textbox is "10/29/2012" which is not a parsable date. How can I use start.setDate and get the value of the textboxes date ?

1
  • 1
    Date.parse should be more than able to parse this format 10/29/2012. Just try Date.parse('10/29/2012') in your browser console and you'll see it gets parsed Commented Oct 27, 2012 at 20:42

3 Answers 3

3

If the value of the textbox is something like "10/29/2012", you should be able to use:

var new_start = new Date($('#<%= tStartDate.ClientID %>').val());

But if you're trying to use setDate, the value that it accepts is an integer from 1-31, so you need to use getDate on the new Date object, like:

var new_start = new Date($('#<%= tStartDate.ClientID %>').val());
start.setDate(new_start.getDate());
Sign up to request clarification or add additional context in comments.

2 Comments

When you use new Date() like this you are giving for granted that they value is in American format... "10/12/2012" will be parsed as the 12th of October but for many countries it corresponds to the 10th of December.
@Tallmaris Actually no, I'm assuming that the OP wants an answer that is tailored to their exact requirements. They are clearly using the American date format, so I provided a solution that uses that. You are right that if they used any other format with the datepicker, this would not work...at least this immediate solution. But it's not like their datepicker will magically detect whether the user wants American date format or another format - the developer, the OP, determines that and that's why this works.
2

Use $(selector).datepicker("getDate") and then .getDate() to grab the day. In your case:

start.setDate($('#<%= tStartDate.ClientID %>').datePicker("getDate").getDate());

As the first comment says, this will give you a Date object directly, regardless of the string format used in the textbox. Have a look at the API documentation :)

Edit: as ianpgall noted, you will need to also use getDate() to grab the day number.

3 Comments

note that will already be a Date object also returned by getDate
Except that start.setDate expects an integer from 1-31, where datePicker("getDate") returns a Date object, so this probably wouldn't work
You are right, but in that case it is enough to add .getDate(). I amended my answer. This should work in any locale.
0

Use Utility functions from datepicker http://docs.jquery.com/UI/Datepicker/parseDate

$.datepicker.parseDate('yy-mm-dd', '2007-01-26');

Comments

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.