admin管理员组

文章数量:1394175

Since I am new to jQuery I have a simple question to the pro-users.

  1. How do I print out on the screen the date from the datepicker?
  2. How to assign the date from datepicker to a variable so I can use it later for using that date's with mySQL database manipulation.

Any help appreciated.

$(function() {
    $("#from").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1,
        dateFormat: "yy/mm/dd",
        showAnim: 'clip',
        onClose: function(selectedDate) {
            $("#to").datepicker("option", "minDate", selectedDate);
        }
    });

    $("#to").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1, maxDate: 0,
        dateFormat: "yy/mm/dd",
        showAnim: 'clip',
        onClose: function(selectedDate) {
            $("#from").datepicker("option", "maxDate", selectedDate);
        }
    });
});

Since I am new to jQuery I have a simple question to the pro-users.

  1. How do I print out on the screen the date from the datepicker?
  2. How to assign the date from datepicker to a variable so I can use it later for using that date's with mySQL database manipulation.

Any help appreciated.

$(function() {
    $("#from").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1,
        dateFormat: "yy/mm/dd",
        showAnim: 'clip',
        onClose: function(selectedDate) {
            $("#to").datepicker("option", "minDate", selectedDate);
        }
    });

    $("#to").datepicker({
        defaultDate: "+1w",
        changeMonth: true,
        changeYear: true,
        numberOfMonths: 1, maxDate: 0,
        dateFormat: "yy/mm/dd",
        showAnim: 'clip',
        onClose: function(selectedDate) {
            $("#from").datepicker("option", "maxDate", selectedDate);
        }
    });
});
Share edited Jan 16, 2018 at 5:18 Clomp 3,3182 gold badges25 silver badges38 bronze badges asked Sep 18, 2013 at 14:02 user2791728user2791728 111 gold badge1 silver badge2 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 2
$( "#from" ).datepicker("getDate"); //to get the date

var date;

date = $( "#from" ).datepicker("getDate"); //to store it in a variable.

The API is useful if you need to do more with your datepicker.

http://api.jqueryui./datepicker/#method-getDate

you can use onSelect method to assign selected value to some variable. You can post that value to your controller and save it to database.

$(function() {

    var selectedDate = "";

    $("#from").datepicker( {
        onSelect: function(date) {
            alert(date);
            selectedDate = date;
        },
        selectWeek: true,
        inline: true,
        startDate: '01/01/2000',
        firstDay: 1
    });
});

Use

getDate

it Returns the current date for the date picker or null if no date has been selected.

var fromDate= $( "#from" ).datepicker( "getDate" );
var toDate= $( "#to" ).datepicker( "getDate" );

本文标签: javascriptjQuery datepickerstoring the date in a variable and printing itStack Overflow