2
votes

If I have an html control in my page.

<input type="text" id="dateFrom" runat="server" ClientIDMode="Static"/>

On a button click, I save the value of input dateFrom to a Sitecore item's field with type "DateTime"

CodeBehind:

item["Visible From"] = dateFrom.Value;

I am using Jquery to show a calendar when user clicks inside the input dateFrom

$(function () {
        $("#dateFrom").datepicker();
    });

After saving the item, the result I see in the "Content Editor" is: enter image description here

The correct date is some how not saving. I also tried with <asp:TextBox instead of <input type="text" but still same result.

2

2 Answers

10
votes

The date is stored in ISO 8601 format in the backend, use the Sitecore helper methods to get it into the correct format:

var dateTime = DateTime.Parse(dateFrom.Value);
var isoDate = DateUtil.ToIsoDate(dateTime);
item["Visible From"] = isoDate ;

You can find more info in this article about Using DateTime And Date Fields In Sitecore

0
votes

The dates are stored in Sitecore in its own (string-based) format. You may check that if you check View --> Raw Vales from a menu above.

Sitecore has its own helper class called DateUtil to work with dates and times.

string storedDateTimeValue = DateUtil.DateTimeToIsoDate(DateTime.Now);

or reverse operation:

DateTime dt = DateUtil.IsoDateToDateTime(storedDateTimeValue);

Here is a bit outdated, but correct information on that DateUtil class API.