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

2

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Just had chance to test - you're right :-) let me have a quick look and I'll get back to you :-)
I could use the altfield/altformat if there was a way to go into a div and not input.
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 richsage.co.uk/bits/jqueryui/ui.html and see if the result is the same?
Odd I copied the code you have on the source and it works fine. No clue what the difference is.

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.