0

I have the following simple datepicker in JqueryUI:

$('#id_due_date').datepicker().datepicker("option", "dateFormat", "yy-mm-dd");
$('#id_due_date').datepicker("setDate",
                             new Date($("input#id_due_date").attr("value")));

So, if the already has a value it shows that date instead of blank (second line of code above).

However, i'm having the following difficulties:

  1. I want to be able to show the month of that initial value in the calendar. So if that date is 3 months ahead, i want to show that month in the calendar.
  2. With this code, if i'm not setting an initial value, it defaults to today, which is kind of wrong for my application as some items can have a missing due_date

Can you please help?

1
  • so what do you want to do if there is no date? set the calendar 3 months ahead? Commented Feb 18, 2011 at 13:30

2 Answers 2

2
  1. new Date is a javascript native method and expects the javascript dateformat to be correct
  2. check out alt field, altField in jq ui
  3. use datepicker({option:value})
  4. check the changeMonth option in jq ui
  5. check the change year option in jq ui
Sign up to request clarification or add additional context in comments.

1 Comment

then add a hidden input and that could have the name attr instead of the one that is show. essentially one is use for display purposes the other for formatting purposes
0

This should work:

$('#id_due_date').datepicker({dateFormat:"yy-mm-dd"});

Or to set the default date 1 month in the future:

$('#id_due_date').datepicker({dateFormat:"yy-mm-dd", defaultDate:'+1m'});

Then, just make sure that the input it's attached to is either blank or in the format of "2011-02-18" (yy = yyyy FYI; see here)

This way, the datepicker will automatically pick up the date in the box and show it. If it's blank, it will just display today's date.

4 Comments

But I don't want to show it today's date because it is null for a reason. Let's say you add a task that doesn't have a due date. It won't be so pretty for every task you add to be by default due'd today.
@rsavu: I have updated it to show how to set the default date to 1 month in the future.
Sorry, but this is a hack to set a due_date in the future. I'm looking for a none | blank | null value :)
@rsavu: I'm confused. These are options for the datepicker, they are not hacks. If you want the date to be blank, why not just have the person delete the value in the input element or have a radio/check box that says "No due date" which will set the input's value to blank using the click event?

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.