So I am currently building an SP custom form which uses the jQuery ui datetimepicker (extension of jQuery ui) which allows me to select dates and times in one control. Once this is done and the form is submitted, the dates are stored in a list. The list item can then be opened in edit mode (edit the list item) and the date is manually populated back into the custom form. Here is where the crux of my issues begin.

Firstly, when I save the dates in the form to the list upon clicking the submit button the date has to be converted or parsed to comply with the Regional Settings in SharePoint, otherwise I immediately receive a "DateTime not in correct format" exception. So I manually parsed that date and then I had issues with saving the date on another SP instance which used a different Regional setting. I then decided to use substring to manually format the date before saving it instead of using the parsing. This worked perfectly and even though the format was correct, upon fetching it back into the form on edit mode, it would change the format.

I have googled and read A LOT of solutions and tried many different things (ParseExact, Parse, Formats, String manipulation, etc) And I cannot find something that is consistent and effective to take care of this issue.

If anyone has any idea or suggestion on how I can perfect this please post some advise and if possible examples since I am not that well versed at date formatting as yet and all the articles I have read have somewhat confused me, there are simply way too many different solutions and I cannot seem to figure out what is the best/right way for me. Thanks in advance, code snippets and my code is below:

When the datetime is selected in the picker the textbox is populated as follows:

28/11/2014 11:00 (<--- 24 hour format)

When the form is submitted, this value is saved in the SP list as:

2014/11/22 09:00 AM (<--- reversed format due to regional settings 12 hour format?)

SaveFormToList Method:

    string datereturn = dpReturn.Text;
    string newreturn = datereturn.Substring(0, 16);
    item["Departure Date"] = Convert.ToDateTime(newdeparture);

This above above method no longer works for my saving now since I switched from parsing to string manipulation and the SPListField is a DateTime field (not a string) - using Convert.ToDate - even though i change the string to the same date format as when the timepicker is used it still tells me that the DateTime format was not correct....

My LoadListItem Method (gets the list item and populated my textbox with the date saved using above method)

 DateTime departureget = Convert.ToDateTime(currentItem["Departure Date"]);
                string nedep = departureget.ToString("dd'/'MM'/'yyyy HH:mm");
dpDeparture.Text = nedep;

js for DateTimePicker: (stock standard)

//Initialize Date Time Fields
function initializeDateControls() {
        var _dateOfDeparture = $(".dpDeparture");   
            _dateOfDeparture.datetimepicker({ showOn: "both", buttonImage: "/_layouts/calendaricon.gif", buttonImageOnly: true, dateFormat: "dd/mm/yy", timeFormat: 
"HH:mm" });

The main problem is that saving and reading the date is very inconsistent since the formats always end up being different (changed by sharepoint) upon saving the date and time. How can i prevent these issues now and so that I better understand how to specifically cater for date fields in the future. I would appreciate some assistance from any available experts out here. :)

1 Answer 1


After much ado, I have finally managed to find a pretty fool-proof way (I believe) to take care of the DateTime issue I posted above. This literally took me about a week to get right, although I have been focusing on other tasks, but anyway, without further ado, here is the best solution I found to deal with the SharePoint Regional Settings and forced date conversions that take place without you having any say whatsoever.

Save the DateTime value to the list as follows: (Yes, you HAVE to split each component)

string datedeparture = txtDeparture.Text;
int departday, departmonth, departyear, departhour, departminute = 0;

string newdepart = datedeparture.Substring(0, 16);
                    departday = Convert.ToInt32(newdepart.Substring(0, 2));
                    departmonth = Convert.ToInt32(newdepart.Substring(3, 2));
                    departyear = Convert.ToInt32(newdepart.Substring(6, 4));
                    departhour = Convert.ToInt32(newdepart.Substring(11, 2));
                    departminute = Convert.ToInt32(newdepart.Substring(14, 2));

//Build the date using each component and remember to add the seconds with a 0
DateTime departuredate = new DateTime(departyear, departmonth, departday, departhour, departminute, 0);

//Update your SPListItem
item["Departure Date"] = departuredate;

When you READ/DISPLAY the date on your form (so when you fetch it from your list) - do the following:

DateTime departureget = Convert.ToDateTime(currentItem["Departure Date"]);
//Now change the format back to what you need (and to the SAME format that the jquery-ui DateTimePicker uses.
string newdep = departureget.ToString("dd'/'MM'/'yyyy HH:mm");
txtDeparture.Text = newdep;

Finally it works on multiple environments! If you have a wonky regional setting then you need to modify the substring methods to read in the correct indexes from your date string. Anyway glad I managed to figure this out on my own. Any advice or suggestions are still most welcome. Have a nice day! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.