0

I get an array with dates as string from the server, now I want to filter only day, month and year. How can I format the filter result to a certain date format?

var date  = ['2015-02-04T23:54:00.000+01:00','2015-02-04T23:54:00.000+01:00', ...];

//wanted result: 2015-02-04 or 04.02.2015
2
  • 2
    How about using new Date(STRING); in loop ? And then, dateObj.getMonth(), dateObj.getFullYear()... Commented Mar 28, 2016 at 12:48
  • you can use moment for this Commented Mar 28, 2016 at 12:49

4 Answers 4

1

You could convert your what's look to be an ISO Date format like this:

var date  = ['2015-02-04T23:54:00.000+01:00','2015-02-04T23:54:00.000+01:00'];

date.map(function(_d) {
    var d = new Date(_d)
    return d.getFullYear() + '-' + d.getMonth() + 1 + '-' + d.getDay()
}

// if you want to get fancy, you could throw in this function to pad the days and months:
var pad = function (n) {return n<10? '0'+n:''+n;}

var sorted = date.map(function(_d) {
    var d = new Date(_d)
    return d.getFullYear() + '-' + pad(d.getMonth() + 1) + '-' + pad(d.getDay())
})
console.log(sorted);
Sign up to request clarification or add additional context in comments.

3 Comments

You can try return d.toISOString().split('T')[0]
If I use your suggestion, here is what I obtain : 2015-02-04. Using mine, I have 2015-02-03. How do you cope with the timezone part (T23:54:00.000+01:00)?
Well it works fine for me: JSFiddle. I have tried few timezones, but not sure.
1

Date can take an argument of a string. Use a for loop to iterate through your list, and then make a new Date object for each one.

var date  = ['2015-02-04T23:54:00.000+01:00','2015-02-04T23:54:00.000+01:00']
var dateObjects = [];

for (var i = 0; i<date.length; i++) {
  d = new Date(date[i]);
  dateObjects.push(d);
}

Or, in a single line:

var dateObjects = date.map( function (datestr) {return new Date(datestr)} );

Now, you can find the month, day, and year from one of these by the following methods:

var year = dateObjects[0].getFullYear(); // Gets the year
var month = dateObjects[0].getMonth()+1; // Gets the month (add 1 because it's zero-based)
var day = dateObjects[0].getDate(); // Gets the day of the month

dateObjects[0] is just an example that refers to the first date in the list.

So you can then get your output string like

var dateStrings = dateObjects.map(function (item) {
  return item.getFullYear()+"-"+(item.getMonth()+1)+"-"+item.getDate();
})

1 Comment

You can try Array.map for this. var dateObj = date.map(function(item){return new Date(item);});
1
var date  = ['2015-02-04T23:54:00.000+01:00','2015-02-04T23:54:00.000+01:00'];
var newdateobject = [];

$.each( date, function(key, e) {
var a = new Date(e);
  newdateobject.push(a.getFullYear()+'-'+(a.getMonth()+1) +'-'+a.getDate());
});

Comments

0

IF the format you mentioned is consistent, then:

date.forEach(function(d) {
    d = d.substring(0, 10);
})

2 Comments

Will work in most of the cases but what if date is 2015-2-4T23:54:00.000+01:00 ?
as I said, if the format is consistent. If the format is inconsistent, then I don't think there is any cross browser compliant solution. Even new Date's implementation is inconsistent in case of converting strings to dates.

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.