从string / django对象获取datepicker defaultdate [英] Get datepicker defaultdate from string/django object

查看:309
本文介绍了从string / django对象获取datepicker defaultdate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在jquery和Javascript中总共(比如没有经验)noob,我想做的是:



我通过了文章对象到我的模板,其中存储有日期信息。
我可以使用 {{article.pubdate}} 访问并显示该信息。
是否可以在文章日期设置datepicker的 defaultDate 选项?

我该怎么做?



感谢任何指针!



更新
有关 {{article.pubdate}}的更多信息

它是一个datetime对象。可以编辑字符串的显示格式,所以 {{article.pubdate | date:Ymd}} 给出了2013-07-27的日期。 >

更新2
我在页面上显示文章日期如下:

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

这给了我 2013-07-27 作为页面上显示的字符串。
阅读后,我尝试了以下方法,将该字符串转换为datepicker,如下所示:

 < 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>

但这不行,为什么?



更新2
如果我设置 defaultDate 像这样:

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

它的工作绝对不错。有没有办法将日期信息直接传递给datepicker?

解决方案

您可以使用 setDate



您需要在初始化之后使用datepicker,因为你需要传递日期对象

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

$('#datepicker')。datepicker('setDate',new Date($(#article-pubdate)。text()。replace(/ \- / g,','))) ;


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?

解决方案

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, ',')));

这篇关于从string / django对象获取datepicker defaultdate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆