Compare dates in your Sitecore personalization rule


Hello good people,

First of all I would like to thank Sitecore for awarding me the Sitecore’s Technology MVP 2017. I’m truly honoured.

Today I’m going to share with you how easy it is to create/make a date comparer rule.

In this scenario we will do a pretty dumb rule 🙂
There will be two values(date picker) which will be compared by using the operator condition.

Jeff Darchuk has a great post on how to create/add rules in Sitecore – Lets use that rules engine!
So why not follow his instructions 🙂

1. Create a new tag to identify our new group of rules, we will call it TestRules.

2. Go to path: /sitecore/system/Settings/Rules/Definitions/Elements. Here you will see a bunch of rules, lets create a new Element Group and call it TestRules:

3. Associate the new tag TestRules with the rule element folder(TestRules) by setting the default tag on the rule element.

4. We want the rule to be accessible when the editors are working in the Experience Editor(Presentation Details), let’s add the new tag(TestRules) to the conditional rendering rules:

Now let us create the rule and select the condition alternative:

So now we have a an empty condition rule called – DateComparerRule. Next will be to add some logic to it, we will add the two date fields and a condition comparer.

Here is the “text” code:
when [DateFieldOne,DateTime,,first date] [operatorid,Operator,,compares to] [DateFieldTwo,DateTime,,second date]

Let me give you a quick explanation.
DateFieldOne and DateFieldTwo – These are the properties in our custom rule
DateTime – This is the datepicker
“first date” and “second date” – The texts you want to show the editor, when he/she is using the rule.

operatorid – Will hold what “operator condition” you have selected(greater than, less than etc)
Operator – The operator picker
“compares to” – The text you want to show to the editor, when he/she is using the rule.

Ok so lets see how the rule look and feels for the editors.

Yes it seems to work ok, next will be to add some code for the rule.

Here is the actual code for our custom rule,DateComparerRule. We need to inherit from OperatorCondition in order to compare the two date field properties: DateFieldOne and DateFieldTwo.

public class DateComparerRule<T> : OperatorCondition<T> where T : RuleContext

	public string DateFieldOne { get; set; }

	public string DateFieldTwo { get; set; }

	protected override bool Execute(T ruleContext)
		Assert.ArgumentNotNull((object)ruleContext, "ruleContext");

		if (!IsDate(DateFieldOne) || !IsDate(DateFieldTwo))
			return false;

		ConditionOperator conditionOperator = base.GetOperator();

		return DateComparer(DateUtil.ParseDateTime(DateFieldOne, DateTime.MinValue), DateUtil.ParseDateTime(DateFieldTwo, DateTime.MinValue), conditionOperator);

	private bool IsDate(string date)
		DateTime tempDate;
		return DateTime.TryParse(date, out tempDate);

	private bool DateComparer(DateTime date1, DateTime date2, ConditionOperator conditionOperator)
		switch (conditionOperator)
			case ConditionOperator.Equal:
				return date1.Equals(date2);
			case ConditionOperator.LessThan:
				return date1 < date2;
			case ConditionOperator.LessThanOrEqual:
				return date1 <= date2;
			case ConditionOperator.GreaterThan:
				return date1 > date2;
			case ConditionOperator.GreaterThanOrEqual:
				return date1 >= date2;
			case ConditionOperator.NotEqual:
				return !date1.Equals(date2);
				return false;


In order to get what ConditionOperator the editor has selected(greater than, less than etc.) we use the base method base.GetOperator(). From method DateComparer we will return the outcome of the comparison between the two properties: DateFieldOne and DateFieldTwo.

What we have left is to update the new rule, DateComparer, in Sitecore. We need to tell it where to find the code we have created. We do that by updating field Type(in section Script) with the following:

This is not a big thing but I want you guys to see how easy it is to make your own custom rules.

Keep on personalization out there.

That’s all for now folks 🙂

One thought on “Compare dates in your Sitecore personalization rule

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.