Tuesday, August 3, 2010

Changing dd/mm/yyyy(UK english) to mm/dd/yyyy(US english) in sharepoint programatically

Take one label and one button (by default label will display the current date
if we click on the button it will show us format(mm/dd/yyyy)



string strdate = "";
string startdate = "";
SPWeb spWeb = new SPSite(SPContext.Current.Web.Url.ToString()).OpenWeb();
startdate = System.DateTime.Now.ToString();

Label1.Text = startdate;(To display current date)

//method to change date format dd/mm/yyyy to mm/dd/yyyy


public void GetDates(string date, out string Date)
{
Date = "";
string strYear = "";
string strMonth = "";
string strDay = "";


string[] Dates;
Dates = date.Split('/');
for (int i = 3; i <= Dates.Length; i--)
{
if (i > 0)
{
if (i == 2)
{
if (Dates.GetValue(i - 1).ToString().Length == 2)
{
strMonth = Dates.GetValue(i - 1).ToString();
}
else
{
strMonth = "0" + Dates.GetValue(i - 1).ToString();
}
}
if (i == 3)
{
strYear = Dates.GetValue(i - 1).ToString();
}
if (i == 1)
{
if (Dates.GetValue(i - 1).ToString().Length == 2)
{
strDay = Dates.GetValue(i - 1).ToString();
}
else
{
strDay = "0" + Dates.GetValue(i - 1).ToString();
}


}
}
if (i == 0)
{
Date = strMonth + "/" + strDay + "/" + strYear;
return;
}
}
}





protected void Button1_Click(object sender, EventArgs e)
{
// SPWeb spWeb = new SPSite("http://jagadeeshv:2222").OpenWeb();

if (spWeb.RegionalSettings.LocaleId != 1033)
{
GetDates(startdate, out strdate);
startdate = strdate;
Label1.Text = strdate;
}
else
{
Label1.Text = startdate;
}
}

No comments:

Post a Comment

Content Editor Webpart not showing up in SharePoint Online ------------------------------------------------------------------------------...