user3800799
user3800799

Reputation: 538

How to treat strings in an array as dates

I have an array with dates like so:

Nov 01 2015 22: +0
Nov 01 2015 23: +0
Nov 02 2015 00: +0
Nov 02 2015 01: +0

This is exactly the format I'm getting from the server and I have no control on it. Is it possible to refer all of these as dates? For example, I would like to get only those from November 2.

Upvotes: 0

Views: 66

Answers (1)

GrafiCode
GrafiCode

Reputation: 3374

You can compare each element of your array with a reference date and populate another array with the right dates.

var dates = ['Nov 01 2015 22: +0', 'Nov 01 2015 23: +0', 'Nov 02 2015 00: +0', 'Nov 02 2015 01: +0'];

var referenceDate = Date.parse('Nov 02 2015 00: +0');

var yourDates = [];

function custom(element, index, array) {
    if ( Date.parse(element) >= referenceDate)
    {
    	yourDates.push(element);
    }
}

dates.forEach(custom);

console.log(yourDates);

// THIS WILL OUTPUT:
//
// ["Nov 02 2015 00: +0", "Nov 02 2015 01: +0"]

DEMO: http://jsfiddle.net/w6vz34bb/

Upvotes: 1

Related Questions