SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Guru
    Join Date
    Nov 2000
    Posts
    741
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Displaying current month

    I want a script that will display the current month and year.

    e.g. April 2007

    What javascript would I need to do this?

    Thanks,

    Jon

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Start by creating a Date object. Then use the getMonth and the getFullYear to get what you need.

    Note that getMonth return a number and not the month name. Number will be 0 to 11. Meaning that you will have to create an Array with the month names to be able to output the name.

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You will need to store an object that contains the month names-
    ideally this would be a language dependent object, but I'll stick to English:

    I like to attach a monthnames array, when needed, to the Date object,
    but you can attach it to the window, or any object you like:

    Date.monthnames= ['January','February','March','April','May','June','July',
    'August','September','October','November','December'];

    Then you can generate the current date and return the month from the array and the year from getFullYear():

    var D=new Date();
    var str= Date.monthnames[D.getMonth()]+' '+D.getFullYear();


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •