最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - getFullYear returns year before on first day of year - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Jan 28, 2016 at 1:43 mediartsmediarts 1,5214 gold badges18 silver badges30 bronze badges 3
  • Wele to the painful, painful world of dealing with timezones. Remend that you never ever ever use anything other than UTC internally and just alter to display to the user. – Jared Smith Commented Jan 28, 2016 at 1:49
  • Do not ever parse strings with the Date constructor as the behaviour is largely implementation dependent. The format in the OP might be treated as UTC, local or NaN depending on which version of which browser it is parsed in. Always manually parse strings (a library can help but is rarely necessary). – RobG Commented Jan 28, 2016 at 2:47
  • Probably not an exact duplicate, but almost: Date code not working in JS. – RobG Commented Jan 28, 2016 at 2:53
Add a ment  | 

3 Answers 3

Reset to default 13

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().

As others have said, your issue is that the host is parsing '2012-01-01' as if you are in GMT and the time is 00:00:00. However, your system is set for a timezone that is west of Greenwich, and that is taken into account when getting the year of the date so you get a date for 31 December in the previous year.

A fundamental rule is to never parse strings using the Date constructor (or Date.parse, which does the same thing) as it is largely implementation dependent and inconsistent. An ISO 8601 date without a timezone should be treated as local according to ISO. However, in ECMAScript ed 3 it could be treated as anything (IE up to and including version 8 didn't parse it at all). Then ES5 said to treat it as UTC (in conflict with ISO). Then ECMAScript 2015 seemed to infer treating it as local, which browsers started to do, but then TC39 said treat it as UTC, which is what most modern browsers do (but there are still plenty of older browsers around that don't).

So if you want consistency, parse date strings manually, e.g. to treat an ISO 8601 date as local, use a function like:

/*  Parse ISO 8601 date string without time zone
**  as a local date
**  @param {string} s - date string to parse in format yyyy-mm-dd
**  @returns {Date}   - local Date, or invalid date if any value is
**                      out of range
*/
function parseISOLocal(s) {
  var b = s.split(/\D/);
  var d = new Date(b[0], --b[1], b[2]);
  return d && d.getMonth() == b[1]? d : new Date(NaN);
}

var s = '2012-01-01';

document.write(parseISOLocal(s))

new Date(dateString) will return what time it was in Greewich England at that time. You could do this if you want the year you seek:

new Date('2012-01-01T00:00:00').getFullYear();

Maybe you like this:

function dateFromString(dateString){
  return new Date(dateString+'T00:00:00');
}
console.log(dateFromString('2012-01-01').getFullYear());
发布评论

评论列表(0)

  1. 暂无评论