I have resolved the issue. I would like to add my answer here
My computed field config
<field fieldName="event_start_date" sourceField="StartDate" returnType="string">Foundation.Search.ComputedFields.DateFieldComputedField, Foundation.Search</field>
<field fieldName="event_end_date" sourceField="EndDate" returnType="string">Foundation.Search.ComputedFields.DateFieldComputedField, Foundation.Search</field>
Computed field logic
public object ComputeFieldValue(IIndexable indexable)
{
var indexableItem = indexable as SitecoreIndexableItem;
if (indexableItem == null) return null;
if (string.IsNullOrEmpty(SourceField)) return null;
var field = indexableItem.Item.Fields[SourceField];
if (field == null) return null;
return Getvalue(field);
}
private DateTime Getvalue(Field fieldValue)
{
DateTime serverTime = DateUtil.IsoDateToDateTime(fieldValue.Value, DateTime.MinValue);
return DateUtil.ToUniversalTime(serverTime);
}
SXA Token logic
public class ExcludePastEvents : ResolveSearchQueryTokensProcessor
{
protected string TokenPart { get; } = "ExcludeItemWithPreviousDateInDateField";
private readonly string DateCompareIdentifier = "#datecompare#";
[SxaTokenKey]
protected override string TokenKey => FormattableString.Invariant(FormattableStringFactory.Create("{0}|FieldName", (object)this.TokenPart));
public override void Process(ResolveSearchQueryTokensEventArgs args)
{
if (args.ContextItem == null)
{
return;
}
for (int index = 0; index < args.Models.Count; index++)
{
SearchStringModel model = args.Models[index];
if (model.Type.Equals("sxa") && ContainsToken(model))
{
string fieldName = model.Value.Replace(TokenPart, string.Empty).TrimStart('|');
args.Models.Insert(index, this.BuildModel(fieldName));
args.Models.Remove(model);
}
}
}
protected virtual SearchStringModel BuildModel(string fieldName)
{
try
{
Assert.ArgumentNotNull(fieldName, "fieldName");
string fieldFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fff'Z'";
DateTime dateTime = DateUtil.ToUniversalTime(DateTime.UtcNow);
string formatedDate = string.Format(arg1: "*", format: "{2}[{0} TO {1}]", arg0: dateTime.ToString(fieldFormat), arg2: DateCompareIdentifier);
return new SearchStringModel("custom", FormattableString.Invariant(FormattableStringFactory.Create("{0}|{1}", (object)fieldName.ToLowerInvariant(), formatedDate)))
{
Operation = "must"
};
}
catch (Exception ex)
{
Log.Info(string.Format("BuildModel - error-{0}stacktrace{1}", ex.Message.ToString(), ex.StackTrace.ToString()), ex);
}
return new SearchStringModel();
}
protected override bool ContainsToken(SearchStringModel m)
{
return Regex.Match(m.Value, FormattableString.Invariant($"{TokenPart}\\|[a-zA-Z ]*")).Success;
}
}