jQuery datetimepicker - how to get date in javascript?

谁说胖子不能爱 提交于 2020-01-15 18:47:23

问题


So I have a jQuery DateTimePicker v2.4.1 attached on a text input. I wanted to make it so then when the user clicks and changes the Month or Year of the picker - it would update the date within the form as this doesn't seem to be a default behavior of the plugin.

    $(timepicker).datetimepicker({
        format: "d M Y h:i A",
        step: 15,
        validateOnBlur: false, //use ValidateDateTime.js instead of jquery default
        closeOnInput: true,
        closeOnDateSelect: true,
        onClose: function() {
            var validatedDate = validateDate($(timepicker).val());
            if (validatedDate != false) {
                $(timepicker).val(validatedDate);
            }
        },
        changeMonth: true,
        changeYear: true,
        onChangeMonth: function() {
            //pop name of month into field and update calendar
            var d = $(this).datetimepicker('getDate');
            console.log(d + this.html());

            /*
            $(timepicker).datetimepicker.trigger("select.xdsoft", [d]);
            */
        }
    });

A problem that occurs is: var d = $(this).datetimepicker('getDate'); does not actually retrieve the date and just returns the datetimepicker element.

How do I get the date in js or more specifically - how could I force an update when the user clicks on a month within the drop down.

EDIT: Looking at the documentary http://xdsoft.net/jqplugins/datetimepicker/ & thanks to suish it now looks like this:

onChangeMonth: function(date, $el) {
    //pop name of month into field and update calendar
    var str = dateTimeToString(date);
    $($el).datetimepicker({ value: str });
},

Simple!


回答1:


onChangeMonth function(current_time,$input){}
onChangeYear function(current_time,$input){}
onChangeDateTime function(current_time,$input){}

According to its documentation.each onChange functions get Date variable as a first parameter.

    onChangeMonth: function(date,$el) {
        var d = date.getDate();
        //d is selected date.
        var m = date.getMonth() + 1; //+1 because .getMonth() returns 0-11
        //m is selected month.
    }


来源:https://stackoverflow.com/questions/29201624/jquery-datetimepicker-how-to-get-date-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!