KeenEyeLearner Posted February 17, 2006 Share Posted February 17, 2006 hi. i am a new js programmer and have some problem with date formatting.problem : given an integer between 1 - 12, the program should give the respective month as output.example: input : 5 ---------> output : Mayi tried searching a lot and could not find a function in js that could solve this problem. All i could find was arrays ( with elements being the month names ) being used. Question : Is there such a date formatting function in java script? if yes, which one? Link to comment Share on other sites More sharing options...
Pollux Posted February 17, 2006 Share Posted February 17, 2006 Theres no built in javascript function that I know of, but if you've got an array with the elements being the month names then writing your own will be trivial. function getMonthByName(numericMonth){ ...your array here... return yourMonthArray[numericMonth-1];} Where 'yourMonthArray' is the name of your array. The -1 is because javascript arrays start at 0, where as the months start at 1. All we're doing is returning the contents of the right element of the array based on the number passed in as a parameter.Just call the function as you would a built in function: getMonthByName(5); which should return "May". Link to comment Share on other sites More sharing options...
KeenEyeLearner Posted February 17, 2006 Author Share Posted February 17, 2006 I know the array solution ( which you have explained here ) but still thanks for the effort. I am clear on the problem now ( since there is no built in function for formatting dates ). A step ahead for me..... Link to comment Share on other sites More sharing options...
mloseke Posted February 18, 2006 Share Posted February 18, 2006 I used the following script to dynamically display the date on a page recently. function showDate(){ var d = new Date() var n = (d.getMonth() + 1); switch(n){ case 1: document.write("January ") break; case 2: document.write("February ") break; case 3: document.write("March ") break; case 4: document.write("April ") break; case 5: document.write("May ") break; case 6: document.write("June ") break; case 7: document.write("July ") break; case 8: document.write("August ") break; case 9: document.write("September ") break; case 10: document.write("October ") break; case 11: document.write("November ") break; case 12: document.write("December ") break; } document.write(d.getDate()) document.write(", ") document.write(d.getFullYear())} employing the switch statement offers an alternative to arrays if for some reason you're against using them. You have to add a 1 to d.getMonth() because by default January is stored at 0.check http://www.lasallebank.com to see it in action Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now