2

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.

Mairaj Ahmad
  • 13,670
  • 2
  • 24
  • 40
shwink
  • 199
  • 1
  • 11

6 Answers6

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

Hope it helps..

Justin
  • 91
  • 1
  • 7
0

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

Community
  • 1
  • 1
Sri
  • 1,117
  • 6
  • 11
0

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/

Rajesh Patel
  • 1,725
  • 13
  • 19
Thusitha Indunil
  • 734
  • 1
  • 6
  • 19
0

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());
Jerome
  • 1,032
  • 10
  • 27
0

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>
Nitheesh
  • 6,950
  • 1
  • 12
  • 31
0

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);
            }
    });  
Rajesh Patel
  • 1,725
  • 13
  • 19