3

I have a datetime value in a component's metadata that I need to access and write out in a component design using the Razor Mediator. Since this is a non-mandatory value I really need to check if it's populated, but as is mentioned in this blog post, datetime in Tridion is non-nullable meaning it simply defaults to the datetime.first value if not populated in a component.

I tried something similar to the suggestion made in the blog post, using the "HasField()" method from Razor documentation instead of "Contains()" as you would in TOM.Net. However, even though I've confirmed in Template Builder that the component object's metadata does not contain an entry for this datetime, it still evaluates as True in "HasField()".

I've got a solution that I'll post, but I'm thinking there's a better option and I'd be curious if someone has a more elegant way to do it, or perhaps could explain why "HasField()" behaves the way it does.

3

Since you know that the default value for a datetime in Tridion is the minimum value of the datetime object, you can do a comparison like:

if(!Metadata.Date.Equals(DateTime.MinValue)){ code }

The only practical impact here is that if you ever had a valid date of 1/1/0001 12:00:00 AM it wouldn't show up, but in my use case that is an impossible scenario. Still, it is something I'd like to avoid if someone has a better way to check this field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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