Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse the year from this date string in JavaScript?

Given a date in the following string format:

2010-02-02T08:00:00Z

How to get the year with JavaScript?

like image 306
Pierce Reed Avatar asked Nov 13 '10 00:11

Pierce Reed


People also ask

How do you parse a string in JavaScript?

The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string. An optional reviver function can be provided to perform a transformation on the resulting object before it is returned.

How do you parse a date?

The parse() method takes a date string (such as "2011-10-10T14:48:00" ) and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. This function is useful for setting date values based on string values, for example in conjunction with the setTime() method and the Date object.

How do you find the month and year from a date object?

To get the year, month and day from a date object:Call the getFullYear() method to get the year. Call the getMonth() method to get an integer from 0 (January) to 11 (December). Call the getDate() method to get the day of the month.

How convert dd mm yyyy string to date in JavaScript?

To convert a dd/mm/yyyy string to a date:Split the string on each forward slash to get the day, month and year. Pass the year, month minus 1 and the day to the Date() constructor. The Date() constructor creates and returns a new Date object.


1 Answers

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

var d = new Date('2011-02-02T08:00:00Z');
alert(d.getFullYear());
like image 74
Jason Benson Avatar answered Oct 10 '22 12:10

Jason Benson