来自datepicker的当前日期,月份,年份

时间:2016-12-22 06:13:24

标签: javascript html datepicker

我有以下代码来获取当前日期。

$('#txtSelectedDate').datepicker({
    showButtonPanel: true,
    currentText: "Today:" + $.datepicker.formatDate('dd mm yy', new Date())
});  

如何分别提取到alert日,月,年。

6 个答案:

答案 0 :(得分:2)

var d = new Date();

var date = d.getDate();

var month=d.getMonth()+1;
// we are adding 1 to getMonth Method, becoz it will return 0 to 11

var year=d.getFullYear();

希望有所帮助..

答案 1 :(得分:0)

请尝试,

var today = new Date();
console.log(today.getDate());
console.log(today.getMonth());
console.log(today.getFullYear());

SoF参考 How to format a JavaScript date

答案 2 :(得分:0)

使用指定的altField选项初始化datepicker:

$( ".selector" ).datepicker({
   altField: "#actualDate"
});

使用指定的altFormat选项初始化datepicker:

$( ".selector" ).datepicker({
   altFormat: "yy-mm-dd"
});

请参阅更多详情: -

http://api.jqueryui.com/datepicker/

答案 3 :(得分:0)

所以在浏览网页后你可以这样做:

var date = $.datepicker.formatDate('dd mm yy', new Date());
alert("Day:" + date.getDay() + "month:" + date.getMonth() + "year:" + date.getFullYear());

答案 4 :(得分:0)

试试这个。 您可以使用onSelect事件



<html>

<head>
    
    <link href="http://code.jquery.com/ui/1.9.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
    <input type='text' class='date'>
    <script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
    <script src="http://code.jquery.com/ui/1.9.2/jquery-ui.js"></script>
    <script>
        $(function ($) {

            $("#dataPicker").datepicker({
                onSelect: function (dateText) {
                    display("Selected date: " + dateText + "; input's current value: " + this.value);
                    alert("Day " + (new Date(dateText)).getDate() + " Month: " + (new Date(dateText)).getMonth() + " Year: " +  (new Date(dateText)).getFullYear())
                }
            }).on("change", function () {
                display("Got change event from field");
            });

            function display(msg) {
                $("<p>").html(msg).appendTo(document.body);
            }

        });
    </script>
</head>

<body>
    <div id="dataPicker">

    </div>
</body>

</html>
&#13;
&#13;
&#13;

答案 5 :(得分:0)

我认为你应该这样做

 $('#txtSelectedDate').datepicker({
        showButtonPanel: true,
        currentText: "Today:" + $.datepicker.formatDate('dd mm yy', new Date()),
       onSelect: function(){
                var day = $("#txtSelectedDate").datepicker('getDate').getDate(); 
    alert(day);                
                var month = $("#txtSelectedDate").datepicker('getDate').getMonth() + 1;  
    alert(month);           
                var year = $("#txtSelectedDate").datepicker('getDate').getFullYear();
               alert(year);
            }
    });