Md. Nazmul Hosan
Md. Nazmul Hosan

Reputation: 97

jquery Change date format

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.

Upvotes: 0

Views: 3418

Answers (5)

GautamD31
GautamD31

Reputation: 28773

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

Upvotes: 0

Akhilesh Sharma
Akhilesh Sharma

Reputation: 1628

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

Upvotes: 0

Nono
Nono

Reputation: 7302

Try this it's working for me:

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

Upvotes: 0

Curtis
Curtis

Reputation: 103428

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

http://jsfiddle.net/8RDwQ/

Upvotes: 1

Chubby Boy
Chubby Boy

Reputation: 31072

Please see below.

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

Upvotes: 0

Related Questions