1

for example

var date = '18-02-2015';

How i can get the year? just the year. if i use getFullYear() -> it just process formate MM-DD-YYYY

Sorry for my bad English

2
  • Where does the date come from? The user, is it dynamically created to "today's" date (whenever it runs) or is it hard-coded? Commented Feb 18, 2015 at 4:28
  • The date comes from user who choose manual date . But i just need the year. Commented Feb 18, 2015 at 4:31

7 Answers 7

2

if you are sure that the format is dd-MM-YYYY then split the string and switch it then parse it as a date.

    var date = '18-02-2015';
    var input = date.split("-");
    var dateObject = new Date(input[2] +"-"+ input[1] +"-"+ input[0]);
    console.log(dateObject);

    var year = dateObject.getFullYear();
    //or without parsing simply
    var year = input[2];
Sign up to request clarification or add additional context in comments.

Comments

1

One approach, taking advantage of HTML's <input type="date" /> element, is:

function findYearFrom() {
  // if it's not of 'type="date"', or it has no value,
  // or the value is equal to the default-value:
  if (this.type !== 'date' || !this.value || this.value === this.defaultValue) {
    // we return here
    return false;
  }

  // we get the value of the element as a date, and then
  // call getFullYear() on that value:
  console.log(this.valueAsDate.getFullYear());
  return this.valueAsDate.getFullYear();
}

// binding the named-function as the change event-handler
// for this element:
document.getElementById('demo').addEventListener('change', findYearFrom);
<input id="demo" type="date" value="2015-02-18" />

Comments

1

if you know that the format always is dd-mm-yyyy you can do

var date = "18-02-2015"    
var year = date.substring(date.lastIndexOf("-")+1)

Comments

0

Hope this helps.

var today = new Date();
var dd = today.getDate();
var mm = today.getMonth()+1; //January is 0!
var yyyy = today.getFullYear();
if(dd<10){
    dd='0'+dd
} 
if(mm<10){
    mm='0'+mm
} 
var today = dd+'/'+mm+'/'+yyyy;  

Refer JS Fiddle here

3 Comments

if i use formate "DD/MM/YYYY" function getFullYear it doesnt running, any others way?
you can use substring to get what you want like today.getFullYear().toString().substr(2,2); you can refer the js fiddle
can you give me a example code? please var tgl_pemberian = document.getElementById("TGL_PERUNTUKKAN").value; var years = tgl_pemberian.getFullYear().toString().substr(2,4); alert ('years'); <-- that is right?
0

Have you looked at http://momentjs.com/

This should work for you to get just the year.

var day = moment("18-02-2015", "MM-DD-YYYY");
console.log(day.format("YYYY"));

Comments

0

With RegExp:

var year = function(str) {
  return str.match(/\d{4}$/)[0]; // \d{4}, four digits group from the end
};

alert(year('18/02/2015'));
alert(year('02/18/2004'));

With slice():

var year = function(str) {
  return str.slice(-4);// get last four characters
};

alert(year('01/12/2015'));
alert(year('18/02/2004'));

1 Comment

Thx brother it works in here but in my web isnt did. I am sorry for wrong information My formate date is : '18/02/2015' can you give me a solution please
0

If the date you're working with is simply a string, then all you're trying to do is get the last 4 characters of the string, which make up the year.

Assuming that the date is always in the format you listed, use slice():

var date = '18-02-2015';
var year = date.slice(-4);

If the date needs to be an integer type, then use this instead:

var year = parseInt(date.slice(-4));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.