Question

I have a date in this format: 05-Jun-2013, I just need year and it need to be converted to just 2013, for my validation purpose. How can I do this, please be concerned that, .getFullYear() is not working.

Was it helpful?

Solution 2

By default getFullYear should work. Might be a possibility that you are not having the appropriate object for usage.

You follow this approach

var dateString = "05-Jun-2013";
var dateStringArray = dateString.split("-");

var yearValue = dateStringArray[2];

OTHER TIPS

var d = new Date('05-Jun-2013');
alert(d.getFullYear());

http://jsfiddle.net/8RDwQ/

Please see below.

var dateObj = new Date("05-Jun-2013");
dateObj.getUTCFullYear();

Try this it's working for me:

var d = '05-Jun-2013';
var nd = new Date(d);
alert(nd.getFullYear());

Try with split like

my_date = '05-Jun-2013'; 
var my_arr = my_date.split('-');
alert('The year is ' + my_arr[2]);

Here my_arr is the array and my_arr[2] will represents the year

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top