0

if I had a string like this:

var dte = '2017-07'

how would I convert that to another var with a value of 'July'?

lightweight
  • 3,227
  • 14
  • 79
  • 142

3 Answers3

1

Use the split() function like this:

    var dte = "2017-07";
    var res = dte.split("-");

Res is an array of each part of the split, so access the month number by typing res[1]. Then I would compare the number in a switch-statement and get out the month name.

D.Soderberg
  • 910
  • 5
  • 11
1

var dte = '2017-07'
var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
var another = months[+dte.split('-')[1] - 1]
console.log(another);
dNitro
  • 5,145
  • 2
  • 20
  • 45
0

First you split the string in "yyyy", "-" and "mm". Then you compare the last string, "mm" with paterns that correspond to the correct month. You could do this with 2 string arrays. 1 has the paterns, "01", "02" etc. The other has names, "januari", "februari" etc. Iiterate the for loop over the first array. When you have a match, you know the word you are looking for is in the other array, at the same index.

fstam
  • 669
  • 4
  • 20