The regional settings of the site were set to English (Australia) – see screenshot below.
My calculated column was set up like so (formula and columns blurred out) -
Looking at the list everything appeared fine and the dates were displayed in the correct format (dd/MM/yyyy). Expiry is the calculated column.
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);
Fuente:
http://www.sharepointalex.co.uk/
Más información:
- Definición y ejemplos de fórmulas: http://msdn.microsoft.com/en-us/library/bb862071.aspx
- Uso de fórmulas: http://sharemypoint.in/2009/03/04/sharepoint-calculated-columns-formulas/
No hay comentarios:
Publicar un comentario