views:

493

answers:

1

I have this code

$("#calendar").datepicker({
    dateFormat: 'yy-mm-dd',
    onSelect: function(dateText, inst) { 
     $('.selected-date').html(dateText);
     }

    });

I am looking for a way to send the date in 1 format to the database with ajax(not shown) and also update a div with a different date format. What is the best way to do this?

Update # 1

$("#calendar").datepicker({
    dateFormat: 'yy-mm-dd',
    onSelect: function(dateText, inst) {

  var fmt2 = $.datepicker.formatDate("mm-dd-yy", dateText);
  $(".selected-date").html(fmt2);
    }

    });

Update # 2

$("#calendar").datepicker({
    dateFormat: 'yy-mm-dd',
    onSelect: function(dateText, inst) {
    var d = new Date(dateText);
    var fmt2 = $.datepicker.formatDate("DD, d MM, yy", d);
    $(".selected-date").html(fmt2);



}

    });
+1  A: 

You've already got the function that's called when a date is selected, so you can then do something similar to the following:

onSelect: function(dateText, inst) {
  $('.selected-date').html(dateText);

  var fmt1 = $.datepicker.formatDate("yy-mm-dd", dateText);
  $.get("/your/ajax/url/" + fmt1, yourCallbackFunction);

  var fmt2 = $.datepicker.formatDate("mm-dd-yy", dateText);
  $("#someotherdiv").html(fmt2);
}

Not tested but should work... :-)

Update #1

Looks like the datepicker.formatDate() function needs a Date object to work with. The following works here...

onSelect: function(dateText, inst) {
  $('.selected-date').html(dateText);

  var d = new Date(dateText);

  var fmt1 = $.datepicker.formatDate("dd/mm/y", d);
  $.get("/your/ajax/url/" + fmt1, yourCallbackFunction);

  var fmt2 = $.datepicker.formatDate("MM o, yy", d);
  $("#someotherdiv").html(fmt2);
}
richsage
This does not work.See Update # 1 for code
matthewb
Just had chance to test - you're right :-) let me have a quick look and I'll get back to you :-)
richsage
Hopefully the above update should work now :-)
richsage
I could use the altfield/altformat if there was a way to go into a div and not input.
matthewb
Everything is NaN... See update # 2
matthewb
What browser are you using? I'm on FF3.5, Ubuntu...
richsage
I'm using FF 3.5.2
matthewb
Very bizarre. Not that it should matter, but what versions of jQuery, jQueryUI have you got? 1.3.2, 1.7.2 here. Can you try http://www.richsage.co.uk/bits/jqueryui/ui.html and see if the result is the same?
richsage
Odd I copied the code you have on the source and it works fine. No clue what the difference is.
matthewb
Very bizarre! Glad it works now though :-)
richsage
Yes Thank You! This Works!
matthewb