Skip to content

Instantly share code, notes, and snippets.

@JasonElkin
Last active March 18, 2022 02:01
Show Gist options
  • Save JasonElkin/325538a2d9089ddae99dfdeccc4f5586 to your computer and use it in GitHub Desktop.
Save JasonElkin/325538a2d9089ddae99dfdeccc4f5586 to your computer and use it in GitHub Desktop.
Nullable Date Picker Property Value Converter for Umbraco
public class NullableDatePickerValueConverter : DatePickerValueConverter
{
public override Type GetPropertyValueType(IPublishedPropertyType propertyType) => typeof(DateTime?);
public override object? ConvertSourceToIntermediate(
IPublishedElement owner,
IPublishedPropertyType propertyType,
object source,
bool preview)
{
if (source is string sourceString)
{
var attempt = sourceString.TryConvertTo<DateTime?>();
return attempt.Success == false ? null : attempt.Result;
}
return source as DateTime?;
}
public override object? ConvertIntermediateToXPath(
IPublishedElement owner,
IPublishedPropertyType propertyType,
PropertyCacheLevel referenceCacheLevel,
object inter,
bool preview)
{
var interDate = inter as DateTime?;
if(interDate is null)
{
return null;
}
return XmlConvert.ToString(interDate.Value, XmlDateTimeSerializationMode.Unspecified);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment