Tag Archives: DateTime

Get and format DateTime string from SharePoint DateTime field

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”);

This converts the DateTime string to a standard format which allows it to be understood by various javascript and jQuery functions and plugins, which was exactly what we needed.

Here is a list of other formats you can get with this method by simply replacing the “s” with another letter.

CAML Query Example: Including GroupBy

Just to remember how to write these things.

                <FieldRef Name="myFieldName" />
                <Value Type="DateTime">
                  <Today OffsetDays="6"/>
                <FieldRef Name="myFieldName2"/>
                <Value Type="Choice">myFieldValue</Value>
            Collapse ="TRUE"
            GroupLimit ="100">
            <FieldRef Name="myFieldName">
            <FieldRef Name="MBLEventStart">

Get name of DayOfWeek from DateTime in current SPWeb

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: