22

Given a date in the following string format:

2010-02-02T08:00:00Z

How to get the year with JavaScript?

5 Answers 5

35

It's a date, use Javascript's built in Date functions...

var d = new Date('2011-02-02T08:00:00Z');
alert(d.getFullYear());
Sign up to request clarification or add additional context in comments.

4 Comments

See my comment to @Rob... Hint: Try it on IE8 or below, and even in old Firefox versions, it simply will not work...
Ah thanks! I didn't realize this. That said when dealing with dates I've found it's always a good idea to use the date functions rather than use splits/substr. This is an exception and thanks for the heads up.
Note that browser support is now so universal that using the Date function is best practice.
Method getFullYear() will return the year in the system's local time zone, so if you want the year in local time, it will be correct, but if you want the year printed in the UTC time given, it may be incorrect on either Jan 1 or Dec 31.
12

You can simply parse the string:

var year = parseInt(dateString);

The parsing will end at the dash, as that can't be a part of an integer (except as the first character).

2 Comments

+1, The simplest the better, using the Date object is just an overkill, and should be avoided since the ISO-8601 format is not widely supported -yet-, parseInt will work without problems. I would maybe use also dateString.slice(0,4);
this is bad abstraction and ad hoc.
10

I would argue the proper way is

var year = (new Date('2010-02-02T08:00:00Z')).getFullYear();

or

var date = new Date('2010-02-02T08:00:00Z');
var year = date.getFullYear();

since it allows you to do other date manipulation later if you need to and will also continue to work if the date format ever changes.

UPDATED: Jason Benson pointed out that Date will parse it for you. So I removed the extraneous Date.parse calls.

5 Comments

the "proper" way IMHO for this case is just an overkill, because the format handled by the Date constructor and Date.parse method in ECMAScript 3, are implementation-dependent, the ISO-8601 format was introduced by ECMAScript 5 recently, meaning that you will find implementations that are not able to parse that format. He just wants the first 4 charaacters, I would simply go for dateStr.slice(0,4) ...
@CMS, At the very least, people should know there's an official way, even if it turns out that it doesn't work in IE, or wherever. Parsing dates using slice (or substr or split) is as bad as using a regex to parse xml, even if you don't have a decent xml parser available.
I don't agree: there's no "official" way to get the year of a string formatted this way, a substring will suffice. The only case where a Date object might be handy is when you live in another timezone than GMT, as the current year number differs around the world round New Year's Eve.
Yes, ISO datetime format wouldnt parse() until specifically impemented.
What I'm trying to tell you is that it's not just about IE, Firefox 3.5.x, Chrome 5.0, Safari 4.0, and even the latest Safari 5.0.2 (7533.18.5) to mention a few, will have problems running your snippet. Just compare, a thing as easy as getting the first 4 chars, v.s. having to face a huge number of cross-browser issues. I would say KISS :), unless you need something specific as the case @Marcel mentions.
5
var year = '2010-02-02T08:00:00Z'.substr(0,4)

...

var year = new Date('2010-02-02T08:00:00Z').getFullYear()

Comments

3

You can simply use -

var dateString = "2010-02-02T08:00:00Z";
var year = dateString.substr(0,4);

if the year always remain at the front positions of the year string.

2 Comments

Note that String.prototype.slice is generally preferred to substr, since it has several bugs across implementations, for example in JScript (IE<=8), it can't handle negative indexes. Both are part of the ECMAScript standard, but substr is non-normative, meaning that a standards compliant implementation is not strictly required to implement substr.
@CMS: Wow, I didn't know that, I already wondered why you preferred slice over substr; every day…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.