posted 8/2/2011 by Raghav Khunger
Today I was working with some dates and I encountered an issue while converting date to string. I noticed that inspite of providing the exact format to .ToString(<format>) method the separator was not coming correct after the date was converted to string format. I was using the below code:
string date= DateTime.Now.ToString("M/dd/yy");
and the output I was getting is: As you might have noticed that I passed the separator as '/' but it was coming as '-' in the output string. The reason I found was that I was working with culture which was having '-' as default separator for date. So the solution was to go with overloaded method of ToString(<format>,<Culture>) and to pass the System.Globalization.CultureInfo.InvariantCulture as the second parameter to ToString() method. By doing this my current culture was ignored date was converted to string exactly in the way format was provided.
string date= DateTime.Now.ToString("M/dd/yy", System.Globalization.CultureInfo.InvariantCulture);
Below is the output of above code: Do let me know your feedback, comments.
What kind of email newsletter would you prefer to receive from CodeAsp.Net?18