Custom Date Format with Language using c#

Posted: January 21, 2013 in ASP.net
Tags: ,

Sometimes we require to change the format of date according to current culture. Those who are working on Multilingual websites must be facing this problem .

I`ll Give you A Simple Example How You Can Do This..

______________________________________________________________________________________

I had made this custom function you can use to Change the date format according to culture.

______________________________________________________________________________________

You Have To write this code in your .cs file

public static string GetDateString(string Date, string InputFormat, string OutputFormat, string Lang)
{
string fromformat = “MMMM dd, yyyy”; //Default value of input format
string toformat = “MM/dd/yyyy”;  //Default value of output format
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(Lang);
if (!string.IsNullOrEmpty(InputFormat))
fromformat = InputFormat;
if (!string.IsNullOrEmpty(OutputFormat))
toformat = OutputFormat;
string newdate = System.DateTime.Now.Date.ToString(toformat);
try
{
newdate = System.DateTime.ParseExact(Date, fromformat, System.Threading.Thread.CurrentThread.CurrentUICulture.DateTimeFormat).Date.ToString(toformat);
}
catch (Exception ex)
{
}
return newdate;
}

 

_______________________________________________________________________________________

In this function you have to input 4 parameters. 

1. Your date.

2.Current format of date.

3. Desired format of date.

4. Language.

This will return the date in desired format.

This Is Basic , Now You Can Modify This Code as You Want 🙂

Hope This Post Will Help you Somehow . . :); . Feel Free To Ask Anything You Need.

Waiting For Your Valuable Feedback.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s