Month number to string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    Month number to string

    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?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
  • Janwillem Borleffs

    #2
    Re: Month number to string


    "Christophe r Benson-Manica" <ataru@nospam.c yberspace.org> wrote in message
    news:bn3l3g$59m $1@chessie.cirr .com...[color=blue]
    > 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?
    >[/color]

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

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


    JW



    Comment

    • Dr John Stockton

      #3
      Re: Month number to string

      JRS: In article <3f955856$0$273 5$1b62eedf@news .euronet.nl>, seen in
      news:comp.lang. javascript, Janwillem Borleffs <jw@jwscripts.c om> posted
      at Tue, 21 Oct 2003 18:02:22 :-[color=blue]
      >
      >"Christophe r Benson-Manica" <ataru@nospam.c yberspace.org> wrote in message
      >news:bn3l3g$59 m$1@chessie.cir r.com...[color=green]
      >> 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?
      >>[/color]
      >
      >Just use an array, e.g.:
      > var months = ['January', 'February', ....etc]
      >
      >and then:
      > var monthString = months[new Date().getMonth ()];[/color]

      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.

      --
      © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
      <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
      <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
      <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.

      Comment

      Working...