Today I checked the code of my team mate who was picking month name from database but there was a problem and to rectify it he was using multiple IF Statements. The problem of month name was with its data type which was string & also it was small month name which means instead of January it was Jan & instead of February it was Feb.
The code he was using to convert small month name into complete month name was something like that
if(monthname==”Jan”)
{
monthname=”January”;
}
if(monthname==”Jan”)
{
monthname=”January”;
}
For each month he was using IF Statement & then I tried to optimize the code. Following logic I used to convert the nasty IF statements into optimized code.
using System.Globalization; string monthName = obj[0].month_come_from_database; string completeMonthName = Convert.ToDateTime(monthName + " 01, 1900").ToString("MMMM", CultureInfo.InvariantCulture);That’s it. It will always provide complete month name. Hopefully it will work for you. Happy Coding.
0 comments:
Post a Comment