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
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
Share Improve this question asked Feb 18, 2015 at 4:21 Rezha VellyRezha Velly 672 silver badges7 bronze badges 2-
Where does the
date
e from? The user, is it dynamically created to "today's" date (whenever it runs) or is it hard-coded? – David Thomas Commented Feb 18, 2015 at 4:28 - The date es from user who choose manual date . But i just need the year. – Rezha Velly Commented Feb 18, 2015 at 4:31
7 Answers
Reset to default 2if 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];
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" />
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)
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
Have you looked at http://momentjs./
This should work for you to get just the year.
var day = moment("18-02-2015", "MM-DD-YYYY");
console.log(day.format("YYYY"));
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'));
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));