Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getFullYear returns year before on first day of year

I'm trying to pull just the year out of a date, but for some reason on the first day of the year its returning the year previous.

new Date('2012-01-01').getFullYear()

will return '2011' and

new Date('2012-01-02').getFullYear()

will return '2012'

Any good ideas on what I'm doing wrong? or a fix for this would be helpful.

like image 975
mediarts Avatar asked Jan 28 '16 01:01

mediarts


People also ask

What does getFullYear return?

getFullYear() The getFullYear() method returns the year of the specified date according to local time.

How to get first day of year in JavaScript?

To get the first day of a year, use the Date() constructor to create a date object, passing it a call to the getFullYear() method for the year, 0 for the month, and 1 for the day as parameters. The Date object will contain the first day of the year.

How to get year from new date js?

JavaScript Date getFullYear() getFullYear() returns the full year (4 digits) of a date.

How do you get the year from a new date?

Description. Javascript date getYear() method returns the year in the specified date according to universal time. The getYear is no longer used and has been replaced by the getYear method. The value returned by getYear is the current year minus 1900.


1 Answers

new Date('2012-01-01') will parse the date assuming it's in UTC. The created Date object incorporates your timezone, so it will take that into account when printing out the result of getYear(). If you're in GMT-anything, that means you're going back to the previous year. You can ignore timezones and deal with just UTC by calling Date.prototype.getUTCFullYear().

like image 76
akhaku Avatar answered Oct 08 '22 14:10

akhaku