Monday, March 12, 2018

JAVASCRIPT: Date Current Year

In JavaScript, we can get the current year as follows:
var myDate = new Date('2017-02-12');
console.log(myDate.getFullYear());
Expected Output: 2017
Actual Output: 2017

But, what happens if we test a boundary value with the above function:
var myDate = new Date('2017-01-01');
console.log(myDate.getFullYear());
Expected Output: 2017
Actual Output: 2016

In this case, we need to call getUTCFullYear()
var myDate = new Date('2017-01-01');
console.log(myDate.getUTCFullYear());
Expected Output: 2017
Actual Output: 2017