AndreaNobili
AndreaNobili

Reputation: 42957

How can I extract the year from the datepicker() result in JavaScript?

I am not so into JavaScript and I have a doubt related how to correcly pick the date from a field.

So into my page I have an input tag that take a date

<input id="dataTrasmissioneAde" class="hasDatepicker" type="text" onchange="aggiornamentoDateAg(this.value)" readonly="readonly" value="" maxlength="10" name="dataTrasmissioneAde">

So this input tag would contain something like 25/06/2015 (and now I have the first trivial doubt: is this content of the input tag considered a String or a Date in JavaScript?)

Ok, then I have a JavaScript function that retrieve the content of the previous input tag by:

oDataTrasmissioneAde = $("#dataTrasmissioneAde").datepicker("getDate");
alert("Data trasmissione all'Ade: " + oDataTrasmissioneAde);

The alert() function show something like this:

Data trasmissione all'Ade: Thu Jun 25 2015 00:00:00 GMT+0200 (ora solare Europa occidentale)

So I think, but I am absolutly not true about this assertion, that the input tag having id="dataTrasmissioneAde" contains a String having value 25/06/2015 and that the datepicker() function convert this String into the related date.

Is it true or am I missing something? Is it a pure JavaScript function or is it a JQuery function? It seems to me that the function in which it is used is a classi JavaScript function but searching online it seems to me that datepicker() is a JQuery function?

How can I extract only the year value from the value of the oDataTrasmissioneAde variable (obtained by the use of the datepicker() function) ?

So in the previous case I have to obtain the 2015 value.

Tnx

Upvotes: 0

Views: 1007

Answers (3)

Mitya
Mitya

Reputation: 34556

datePicker() is part of jQuery UI. And yes, calling .getDate() retrieves the set date and turns it into a JavaScript date object.

To get the year:

$("#dataTrasmissioneAde").datepicker("getDate").getFullYear();

Upvotes: 2

Unni Babu
Unni Babu

Reputation: 1814

Try this, tested and works 100%

<script>


    var x = new Date(oDataTrasmissioneAde+"");
        alert(x.getUTCFullYear());   //x.getUTCFullYear() contains year

</script>

Upvotes: 0

Andrew Koeller
Andrew Koeller

Reputation: 36

Try:

oDataTrasmissioneAde = $("#dataTrasmissioneAde").datepicker("getDate");

var myDate = new Date(oDataTrasmissioneAde );

alert ( myDate.getFullYear() );

You can do whatever you want later with the Date object.

Upvotes: 1

Related Questions