Dawid Kwiatoń
Dawid Kwiatoń

Reputation: 167

Change dates to months in array

i have an array which look like this:

var myDates = ["2017-12-01 08:44:49", "2017-12-03 08:54:49", "2018-01-03 10:54:43"]

I want to create new array with name of Moth, ie.

var myMonths = [
    'December', 'March', 'January']
];

How i can achieve this?

I can do it on one string using something like this:

var oneMonth = "2017-12-01 08:44:49";
var str = oneMonth.substring(5,7);

var months = [
    'January', 'February', 'March', 'April', 'May',
    'June', 'July', 'August', 'September',
    'October', 'November', 'December'
];

var saleMonths = months[str-1];
console.log(saleMonths);

Upvotes: 1

Views: 498

Answers (6)

Emil S. Jørgensen
Emil S. Jørgensen

Reputation: 6366

This answer used the Date object to parse the string and fetch the month.

Then it looks up what month to return from an array of month names.

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

function toMonth(dateString) {
  var d = new Date(dateString);
  return months[d.getUTCMonth()];
}
//TEST
var myDates = ["2017-12-01 08:44:49", "2017-12-03 08:54:49", "2018-01-03 10:54:43"];
console.log(myDates.map(function(d) {
  return toMonth(d);
}));

Upvotes: 0

David
David

Reputation: 1174

You just need to parse the string str into a number, then search for the number -1 element in the months array. like this:

var oneMonth = "2017-12-01 08:44:49";
var number= parseInt(oneMonth.substring(5,7));

var months = [
    'January', 'February', 'March', 'April', 'May',
    'June', 'July', 'August', 'September',
    'October', 'November', 'December'
];

var saleMonths = months[number-1];
console.log(saleMonths);

Upvotes: 0

Paul Fitzgerald
Paul Fitzgerald

Reputation: 12129

The following solution should work for you.

Firstly you map over your dates and get each month. Then you map over these months and use the index to get the month as a word based on the months array.

var myDates = ["2017-12-01 08:44:49", "2017-12-03 08:54:49", "2018-01-03 10:54:43"]

var months = [
  'January', 'February', 'March', 'April', 'May',
  'June', 'July', 'August', 'September',
  'October', 'November', 'December'
];

let monthWords = myDates.map(month => {
  return month.substring(5, 7);
}).map(item => {
  return months[parseInt(item) - 1]
});

console.log(monthWords);

Upvotes: 1

Titus
Titus

Reputation: 22474

You can do what you do to one string to all the strings in the array.

An easy way of doing that will be using the map function. Here is an example:

var myDates = ["2017-12-01 08:44:49", "2017-12-03 08:54:49", "2018-01-03 10:54:43"];
var months = [
    'January', 'February', 'March', 'April', 'May',
    'June', 'July', 'August', 'September',
    'October', 'November', 'December'
];

var onlyMonths = myDates.map(date => {
    var monthIndex = date.substr(5, 2) - 1;
    return months[monthIndex];
});

console.log(onlyMonths)

Upvotes: 0

gurvinder372
gurvinder372

Reputation: 68383

Use map and substring (extending your own logic of substring)

myMonths  = myDates.map( s => months[ +s.substring(5,7) - 1 ] ); 

var myDates = ["2017-12-01 08:44:49", "2017-12-03 08:54:49", "2018-01-03 10:54:43"];
var months = [
  'January', 'February', 'March', 'April', 'May',
  'June', 'July', 'August', 'September',
  'October', 'November', 'December'
];
var myMonths  = myDates.map( s => months[ +s.substring(5,7) - 1 ] ); 

console.log( myMonths );

Upvotes: 0

user3483203
user3483203

Reputation: 51165

You can map using toLocaleString

To add a bit more information:

"long" uses the full name of the month, "short" for the short name, and "narrow" for a more minimal version, such as the first letter in alphabetical languages.

You can change the locale to any that you please, and it will use the right name for that language/country.

From this answer.

var myDates = ["2017-12-01 08:44:49", "2017-12-03 08:54:49", "2018-01-03 10:54:43"]
const months = myDates.map(d => new Date(d).toLocaleString("en-us", { month: "long" }))
console.log(months)

Upvotes: 5

Related Questions