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
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.
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();
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Bookmarks