domingo, 22 de enero de 2012

Columnas calculadas–formato de fechas (format of dates in SharePoint Calculated Columns)


The regional settings of the site were set to English (Australia) – see screenshot below.

SharePoint Regional Settings

My calculated column was set up like so (formula and columns blurred out) -

SharePoint Calculated Column

Looking at the list everything appeared fine and the dates were displayed in the correct format (dd/MM/yyyy). Expiry is the calculated column.

SharePoint List

However when accessing the list items programmatically and trying to convert the Expiry date into a DateTime it was throwing an exception. The dates being returned programmatically were in the below format -

12/15/2011 10:00:00

This is clearly an invalid date when compared to the culture of my machine (UK).

What I ended up doing was converting the date using US culture (en-US) and then changing the format before converting back into a DateTime.

Below is the code snippet that I used to do this – does SharePoint always store the dates for calculated columns in the Content DB as US format?

IFormatProvider provider = new CultureInfo("en-US");

DateTime date = Convert.ToDateTime(myItem["DateColumn"].ToString(), provider);

string convertedDate = date.ToString("dd/MM/yyyy");

DateTime date1 = Convert.ToDateTime(convertedDate);



Más información:

No hay comentarios:

Publicar un comentario