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'?

1
  • Err... '2017-07' is not a Date object... Commented Aug 28, 2016 at 20:35

3 Answers 3

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);

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.