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.
getFullYear() The getFullYear() method returns the year of the specified date according to local time.
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.
JavaScript Date getFullYear() getFullYear() returns the full year (4 digits) of a 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.
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()
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With