2

I am a total (like no-experience-whatsoever) noob in jquery and Javascript, and what I would like to do is the following:

I pass an article-object to my template that has date-information stored in it. I can access and display that information with {{ article.pubdate }}. Is it possible to set the defaultDate option of datepicker to the article date?
How would I go about that?

Thanks for any pointers!

Update: Some more information about {{ article.pubdate }}:
It is a datetime object. The display format of the string can be edited, so {{ article.pubdate|date:"Y-m-d"}} gives a date like 2013-07-27.

Update 2: I display the article date on the page like this:

<div id="article-pubdate">
        {{ article.pubdate|date:"Y-m-d" }}
</div>

This gives me 2013-07-27 as a string displayed on the page. After reading around I tried the following approach to get that string into datepicker like so:

<script>
  $(function () {
      var a = $("#article-pubdate").text();
      var the_date = $.datepicker.parseDate("yy-mm-dd", a);
      $(".datepicker input").datepicker({
          dateFormat: "yy-mm-dd",
          changeMonth: true,
          changeYear: true,
          yearRange: '1990:2013',
          defaultDate: the_date,
      });
  });
</script>

But this doesn't work, why?

Update 2: If I set defaultDate like so:

<script>
  $(function() {
    $( ".datepicker input" ).datepicker({
         dateFormat: "yy-mm-dd",
         changeMonth: true,
         changeYear: true,
         yearRange:'1990:2013',
         defaultDate:"2001-01-01"
    });
  });
</script>

it works absolutely fine. Is there any way to pass the date info directly into datepicker?

LukasKawerau
  • 1,071
  • 2
  • 23
  • 42
  • And what does `article.pubdate` look like? Is it a string, if so in what format, is a Date object, or ... – adeneo Jul 27 '13 at 12:32

1 Answers1

1

You can set default date using setDate

You need to use this after initialization of datepicker, for that you need to pass date Object

$('#datepicker').datepicker(datepicker({
      dateFormat: "yy-mm-dd",
      changeMonth: true,
      changeYear: true,
      yearRange: '1990:2013',
  });

$('#datepicker').datepicker('setDate', new Date($("#article-pubdate").text().replace(/\-/g, ',')));
kartheek
  • 6,434
  • 3
  • 42
  • 41
  • Thanks! But how do I get `{{article.pubdate}}`or the string of it into `new Date()`? – LukasKawerau Jul 27 '13 at 23:08
  • 1. We need to extract the date from div 2. we need to replace the "-" with "," with replace(). i have updated that in ans – kartheek Jul 28 '13 at 05:45
  • For some reason that doesn't work :( Updated my question though, maybe that helps? – LukasKawerau Jul 28 '13 at 08:09
  • have u tried with "setDate" i didnt found in your updated ans, and for the working example [please check this fiddle](http://jsfiddle.net/kartheek527/QjN8R/3/) – kartheek Jul 28 '13 at 11:19
  • The fiddle did it for me, I forgot to change your '#datepicker' to my '.datepicker input' :D Awesome, thank you so much! – LukasKawerau Jul 28 '13 at 11:35
  • Hey, one problem has come up: I can't change the input now. The input field gets set correctly, but I can't change the input field, as datepicker always reverts to the `setDate` value. Any way to change this? – LukasKawerau Aug 13 '13 at 06:10
  • oh damn, my fault, sorry! – LukasKawerau Aug 15 '13 at 11:51