Jump to content
KeenEyeLearner

Formatting Date

Recommended Posts

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? :)

Share this post


Link to post
Share on other sites

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".

Share this post


Link to post
Share on other sites

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..... :)

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...