Using C#. I have a string dateTimeEnd.
If the string is in right format, I wish to generate a DateTime and assign it to eventCustom.DateTimeEnd
It looks like you just want:
eventCustom.DateTimeEnd = string.IsNullOrWhiteSpace(dateTimeEnd)
? (DateTime?) null
: DateTime.Parse(dateTimeEnd);
Note that this will throw an exception if dateTimeEnd isn't a valid date.
An alternative would be:
DateTime validValue;
eventCustom.DateTimeEnd = DateTime.TryParse(dateTimeEnd, out validValue)
? validValue
: (DateTime?) null;
That will now set the result to null if dateTimeEnd isn't valid. Note that TryParse handles null as an input with no problems.