OrchardCMS/OrchardCore · error · ParseException

Could not parse date

Error message

Could not parse date

What it means

DateTimeParser is the Liquid/template date parser used by AuditTrail query helpers. It throws ParseException when the input string matches none of the registered parsers (now, today, or explicit date formats). The library throws this because after exhausting all alternative grammar rules there is no valid parse left at the current cursor position.

Solutions

  1. Use one of the supported literal values: 'now' or 'today'.
  2. Format the date in the parser's expected format (e.g. ISO 'yyyy-MM-dd').
  3. Pre-validate the input with DateTime.TryParse (with the correct culture) before passing it to the parser.
  4. If you need additional formats or relative dates, extend the parser grammar rather than post-processing the exception.

Example fix

// before
var filter = "yesterday"; // throws ParseException
// after
var filter = DateTime.Today.AddDays(-1).ToString("yyyy-MM-dd");
Defensive patterns

Strategy: validation

Validate before calling

var known = new[] { "now", "today" };
bool ok = known.Contains(input, StringComparer.OrdinalIgnoreCase) || DateTime.TryParseExact(input, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out _);
if (!ok) throw new FormatException($"Unsupported date expression: {input}");

Type guard

static bool IsParsableDate(string? s) => s is not null && (s.Equals("now", StringComparison.OrdinalIgnoreCase) || s.Equals("today", StringComparison.OrdinalIgnoreCase) || DateTime.TryParse(s, CultureInfo.InvariantCulture, DateTimeStyles.None, out _));

Try / catch

try { node = parser.Parse(input); }
catch (ParseException ex) { logger.LogWarning(ex, "Bad date input: {Input}", input); return Results.BadRequest(new { error = "Invalid date format" }); }

Prevention

When it happens

Trigger: Calling a template/query filter that parses a date string (e.g. filtering audit trail events by date) with a value that is neither 'now', 'today', nor a recognized date format such as 'yyyy-MM-dd'. Any typo, extra whitespace pattern, or locale-formatted date falls through to this exception.

Common situations: Users typing dates like '01/02/2024' in a locale the parser does not accept, passing 'yesterday' or other relative keywords that are not implemented, or query string values copied with trailing characters.


AI-assisted analysis of OrchardCMS/OrchardCore@4306c0717f (2026-09-13). Data as JSON: /api/errors/99649b0dd59e6cb7. Report an issue: GitHub.

Appendix: source

Thrown at src/OrchardCore.Modules/OrchardCore.AuditTrail/Services/DateTimeParser.cs:221

                {
                    var success = true;
                    if (!DateTime.TryParse(dateValue, context.CultureInfo, DateTimeStyles.None, out var dateTime))
                    {
                        if (!DateTime.TryParse(dateValue, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
                        {
                            success = false;
                        }
                    }

                    // If no timezone is specified, assume local using the configured timezone.
                    if (success)
                    {
                        var converted = context.Clock.ConvertToTimeZone(dateTime, context.UserTimeZone);
                        return new DateNode2(converted.UtcDateTime.Date);
                    }
                }

                throw new ParseException("Could not parse date", context.Scanner.Cursor.Position);
            });

        var currentParser = OneOf(nowParser, todayParser);

        var valueParser = OneOf(currentParser, dateParser);

        var rangeParser = valueParser
            .And(ZeroOrOne(range.SkipAnd(OneOf(currentParser, dateParser))))
            .Then<ExpressionNode>(x =>
            {
                if (x.Item2 == null)
                {
                    return new UnaryExpressionNode(x.Item1);
                }

                else
                {
                    return new BinaryExpressionNode(x.Item1, x.Item2);

View on GitHub (pinned to 4306c0717f)