hexojs/hexo · error · Error

`${value}` is not a valid date!

Error message

`${value}` is not a valid date!

What it means

Thrown by SchemaTypeMoment.validate() (lib/models/types/moment.ts:33). This is the warehouse schema type for date fields (e.g. post.date, post.updated). It converts the value to a moment object via toMoment(); if moment.isValid() is false, the value cannot be stored as a date and the save/validate fails.

Source

Thrown at lib/models/types/moment.ts:33

  constructor(name, options = {}) {
    super(name, options);
  }

  cast(value?, data?) {
    value = super.cast(value, data);
    if (value == null) return value;

    return toMoment(value);
  }

  validate(value, data?) {
    value = super.validate(value, data);
    if (value == null) return value;

    value = toMoment(value);

    if (!value.isValid()) {
      throw new Error('`' + value + '` is not a valid date!');
    }

    return value;
  }

  match(value, query, _data?) {
    return value ? value.valueOf() === query.valueOf() : false;
  }

  compare(a?, b?) {
    if (a) {
      if (b) return a - b;
      return 1;
    }

    if (b) return -1;
    return 0;
  }

View on GitHub (pinned to 059cb17494)

Solutions

  1. Open the offending post and set date to a valid ISO value: date: 2024-01-15 10:30:00.
  2. If the date is variable, validate it with moment(value).isValid() before assigning.
  3. Search the source for the failing value (the error prints it) and fix every occurrence.
  4. Remove the date field to let Hexo default it rather than storing an invalid one.

Example fix

# before (front-matter)
date: 2024-13-40

# after
date: 2024-01-15 10:30:00
Defensive patterns

Strategy: validation

Validate before calling

const moment = require('moment');
const m = moment(value);
if (!m.isValid()) {
  throw new Error(`Invalid date value: ${JSON.stringify(value)}`);
}
post.date = m.toDate();

Type guard

const isValidDate = (v: unknown): boolean => {
  if (v == null) return false;
  const m = require('moment')(v);
  return m.isValid();
};

Prevention

When it happens

Trigger: A post's front-matter date field holds an unparseable value: date: foobar, date: 2021-13-40, date: '' (empty in some flows), or a non-date object/string that moment cannot parse. Also fires on programmatic insert({ date: 'not-a-date' }).

Common situations: Hand-edited front-matter with a typo'd date; locale/format mismatch where an expected YYYY-MM-DD is written differently; migrating content with malformed dates; a plugin setting post.date to an invalid string; empty date after a YAML edit.

Related errors


AI-assisted analysis of hexojs/hexo@059cb17494 (2026-08-12). Data as JSON: /api/errors/1519aa9317b5c8a0. Report an issue: GitHub.