A collegue of mine (his blog here) showed me something nice yesterday. We have a webpart which shows how long ago some list items where created by simply getting the DateTime string from the standard “Created” column.
string x = ((DateTime)item[SPBuiltInFieldId.Created]).ToString();
We ran into trouble however, when we used this webpart on a server in a country using a different time format. The problem was easily fixed like so:
string x = ((DateTime)item[SPBuiltInFieldId.Created]).ToString(“s”);
Here is a list of other formats you can get with this method by simply replacing the “s” with another letter.
Just to remember how to write these things.
<FieldRef Name="myFieldName" />
Was working on a method where I wanted to display the name of the selected day in the language of the Site. This could be hard-coded of course, but that is rarely a sustainable solution in the long run. Instead I get the culture from my current SPWeb. The following snippet shows hot to do this:
string dayOfWeekName = DateTime.Now.ToString("dddd", new System.Globalization.CultureInfo(web.UICulture.Name));
Edit: My collegue showed me how I can get my current culture from the CultureInfo class instead: