Month number to string

  • Thread starter Christopher Benson-Manica
  • Start date
C

Christopher Benson-Manica

What would be the best way to convert a month number to its corresponding
string? I.e, 3 -> 'March'. Is there a builtin function or must I use a
lookup table or something?
 
J

Janwillem Borleffs

Christopher Benson-Manica said:
What would be the best way to convert a month number to its corresponding
string? I.e, 3 -> 'March'. Is there a builtin function or must I use a
lookup table or something?

Just use an array, e.g.:
var months = ['January', 'February', ....etc]

and then:
var monthString = months[new Date().getMonth()];


JW
 
D

Dr John Stockton

JRS: In article <[email protected]>, seen in
news:comp.lang.javascript said:
Christopher Benson-Manica said:
What would be the best way to convert a month number to its corresponding
string? I.e, 3 -> 'March'. Is there a builtin function or must I use a
lookup table or something?

Just use an array, e.g.:
var months = ['January', 'February', ....etc]

and then:
var monthString = months[new Date().getMonth()];

That array will give April for the index 3. The OP, starting with the
conventional number for the month, must either subtract 1 or add a dummy
zeroth month : ['', 'Janu... .

The code given is correct, but for a different question.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,170
Latest member
Andrew1609
Top