shwink
shwink

Reputation: 309

Current day , month, year from datepicker

I have the below piece of code for getting me the current date.

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

How do i extract to alert day, month, year separately.

Upvotes: 2

Views: 2761

Answers (6)

Thusitha Indunil
Thusitha Indunil

Reputation: 832

Initialize the datepicker with the altField option specified:

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

Initialize the datepicker with the altFormat option specified:

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

Please refer for more detail:-

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

Upvotes: 0

Justin
Justin

Reputation: 91

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();

Hope it helps..

Upvotes: 2

Rajesh Patel
Rajesh Patel

Reputation: 2036

I think you should do this way

 $('#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);
            }
    });  

Upvotes: 0

Nitheesh
Nitheesh

Reputation: 19986

Try this. You can use the onSelect event

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

Upvotes: 0

Jerome
Jerome

Reputation: 1192

So after looking on the web you can do that :

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

Upvotes: 0

Sri
Sri

Reputation: 1336

Please try,

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

SoF reference How to format a JavaScript date

Upvotes: 0

Related Questions