Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I need to convert the month in number to read month in Words.

I made a javascript function to convert that like below :
C#
function convert(mycontrolclientID)
{
            var ldtDate=mycontrol.value;
            var month=new Array();
            var lstrDay,lstrMonth,lstrYear,lstrMonthinword=1,lstrDate;
            month = ["",'JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'];
            lstrDay = ldtDate.substr(0,2);
            lstrMonth = ldtDate.substr(3,2);
            lstrYear = ldtDate.substr(6,4);
            lstrMonthinword = month[lstrMonth];
            lstrDate = lstrDay + "-" + lstrMonthinword + "-" + lstrYear;
            return lstrDate;

}



All Months are properly converting to Words except '08' and '09';

i.e 08-08-2012 returning 08-undefined-2012 or 08---2012;
But 03-11-2012 is converting correctly to 03-NOV-2012;

Any Tips/Suggestions to fix this :)
Posted

Hi,

Change this line:
JavaScript
lstrMonthinword = month[lstrMonth];

Into this line:
JavaScript
lstrMonthinword = month[parseFloat(lstrMonth)];
 
Share this answer
 
Comments
Nibin22 26-Dec-12 5:14am    
Thanks ProgramFox !!!
Yes , Its working :)
Thomas Daniels 26-Dec-12 5:59am    
You're welcome!
have you checked that,...
when passing date 08-08-2012 it is not being converted as 8-8-2012 because in this case it will split string in wrong way so, month conversion will be wrong

some links... for set date formats using javascript
http://blog.stevenlevithan.com/archives/date-time-format[^]
http://www.w3schools.com/jsref/jsref_obj_date.asp[^]
JavaScript Date Format[^]
http://stackoverflow.com/questions/1056728/formatting-a-date-in-javascript[^]

Happy Coding!
:)
 
Share this answer
 
v2
Comments
Nibin22 26-Dec-12 5:14am    
Thanks :)
Aarti Meswania 26-Dec-12 5:15am    
welcome! :)
Glad to help you! :)
when you are pass 08 to month[08] it will convert '08' to octal that's why you getting undefined

JavaScript
lstrMonthinword = month[parseInt(lstrMonth, 10)];
 
Share this answer
 
Comments
Nibin22 26-Dec-12 7:32am    
Thanks gagan sawaliya !!!
Its Working

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900