jquery datepicker default date
Asked Answered
R

4

13

I am trying to get a attribute of a date picker to pull the date dynamically, however I get uncaught exception errors when I try to set it to a variable.

The errors only occur on pages that do NOT have the calendar (inline). How can I pul the rel tag from the selector without getting this error?

//Event Calendar Home Page and Listing
function calendar_picker () {
$("#calendar-inline").datepicker({
   //defaultDate: $(this).attr('rel'),
    dateformat: 'yy-mm-dd',
    maxDate: '+1y',
    minDate:'-0d',
    hideIfNoPrevNext: true,
    showButtonPanel: false,
    navigationAsDateFormat: false,
    onSelect: function(dateText, inst) {
                 var d = new Date(dateText);
   var fmt1 = $.datepicker.formatDate("yy-mm-dd", d);
   $.ajax({
   type: "POST",
   url: "/events/listing/all/20/",
   dataType: "html",
                        date: "event_date="+fmt1,
   success: function(){
                        window.location.href= "/events/browse/"+fmt1;
    }});}});
}

UPDATE Correct, the commented line is what I am having issues with, What is the correct way to pull the attribute rel from #calendar-inline from inside this. All attempts throw a uncaught error in js

Update 2

function calendar_picker () {
var myDate = new Date($("#calendar-inline").attr('rel'));
    $("#calendar-inline").datepicker({

     dateformat: 'yy-mm-dd',
     defaultDate:myDate,

Solution:

function calendar_picker () {
var myDate = null;
if ($("#calendar-inline").attr('rel') != null) {
     myDate = $.datepicker.parseDate("yy-mm-dd", $("#calendar-inline").attr('rel'));
     }
    $("#calendar-inline").datepicker({

     dateformat: 'yy-mm-dd',
     defaultDate:myDate,
Robichaud answered 29/10, 2009 at 21:15 Comment(0)
C
9

try this:

defaultDate: $("#calendar-inline").attr('rel')

This will attempt to pull the date from the 'rel' attribute and if that doesn't exist it should return null. When null is passed into the datepicker default date it will use today as the default date.

Cumin answered 9/11, 2009 at 18:43 Comment(9)
This does not work, the date must be parsed and it's not. I tried manually putting a date in a string and it went 1 year in advance.Robichaud
What format is the date in the rel attribute? You should be able to create a var like this: var myDate = new Date($("#calendar-inline").attr('rel')); If you pass that variable into the defaultDate it should work... if not, can you post some html?Cumin
The rel data is "2009-11-19" when I pass what you suggest see above for new js code, now it just defaults to today no matter what is in the rel.Robichaud
Any default date prior to today will be changed to today because of the minDate:'-0d' option. And, try: $.datepicker.parseDate("yy-mm-dd", $("#calendar-inline").attr('rel'))Cumin
Your method works but ONLY on the pages where the picker exists, everyplace else I get a js uncaught exception error. Close but not there.Robichaud
var myDate = null; if ($("#calendar-inline").attr('rel') != null) { myDate = $.datepicker.parseDate("yy-mm-dd", $("#calendar-inline").attr('rel')); } Then pass in myDate for the default date.Cumin
There we go, this works, no js errors and defaults correctly. Thank You, You win the bounty lolRobichaud
@David Radcliffe: you may want to update your answer with what you've added in the comments for future reference?Snowstorm
I'm trying this code but it's not working: defaultDate: $.datepicker.formatDate('dd-M-yy', new Date())...any idea why this cud be the case?Hersch
W
8

We can set default date by this:

<script type="text/javascript">
$(function() {               
    $("#date" ).datepicker({
        changeMonth: true,
        changeYear: true,
        yearRange: '1920:2010',
        dateFormat : 'dd-mm-yy',
        defaultDate: new Date(2000,01,01)
    });
});
</script>

See http://jqueryui.com/demos/datepicker/#option-defaultDate

Woodall answered 26/7, 2012 at 11:5 Comment(1)
(2000,01,01) should be (2000,1,1)Grath
F
0

Your question is a little unclear - what I'm assuming is that your problem occurs on the commented out line.

If that's correct then I think your problem is with the "this". I'm not quite sure what you're expecting "this" to be, but inside the function it's going to be the window object.

If I'm on completely the wrong track please update the question with some more information.

Floorwalker answered 29/10, 2009 at 21:41 Comment(0)
E
0

this works for me:

var nowDate = new Date(year, month, day, hours, minutes, seconds, milliseconds);

then in the date picker, you set:

defaultDate: nowDate;
Empathy answered 12/12, 2011 at 5:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.