1

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);
Dawid Kwiatoń
  • 157
  • 2
  • 13
  • parse the substring into a number, then search for the numbers -1 element in the months array. like this: `var number = parseInt(str); var my_month = months[number-1];` – David Jan 09 '18 at 08:40
  • Yes you can do it like the way you mentioned, except that you need to translate the substring to a number first, so that you can use it as array's index. – 31piy Jan 09 '18 at 08:40
  • I believe your initial `myMonths` is incorrect. Shouldn't it be `['December', 'December', 'January']`? – user3483203 Jan 09 '18 at 08:43

6 Answers6

5

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)
user3483203
  • 50,081
  • 9
  • 65
  • 94
1

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);
Paul Fitzgerald
  • 11,770
  • 4
  • 42
  • 54
0

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 );
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
0

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)
Titus
  • 22,031
  • 1
  • 23
  • 33
0

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);
David
  • 1,084
  • 12
  • 36
0

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);
}));
Emil S. Jørgensen
  • 6,216
  • 1
  • 15
  • 28