Last active
March 18, 2022 02:01
-
-
Save JasonElkin/325538a2d9089ddae99dfdeccc4f5586 to your computer and use it in GitHub Desktop.
Nullable Date Picker Property Value Converter for Umbraco
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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