JamesNK/Newtonsoft.Json · error · JsonSerializationException
Cannot convert date value that is before Unix epoch of 00:00
Error message
Cannot convert date value that is before Unix epoch of 00:00:00 UTC on 1 January 1970.
What it means
Thrown by UnixDateTimeConverter.WriteJson when the DateTime/DateTimeOffset, converted to UTC and expressed as seconds since the Unix epoch, is negative and AllowPreEpoch is false (the default). The Unix epoch is 00:00:00 UTC on 1 January 1970; dates before that yield negative seconds and are rejected unless explicitly allowed.
Source
Thrown at Src/Newtonsoft.Json/Converters/UnixDateTimeConverter.cs:97
if (value is DateTime dateTime)
{
seconds = (long)(dateTime.ToUniversalTime() - UnixEpoch).TotalSeconds;
}
#if HAVE_DATE_TIME_OFFSET
else if (value is DateTimeOffset dateTimeOffset)
{
seconds = (long)(dateTimeOffset.ToUniversalTime() - UnixEpoch).TotalSeconds;
}
#endif
else
{
throw new JsonSerializationException("Expected date object value.");
}
if (!AllowPreEpoch && seconds < 0)
{
throw new JsonSerializationException("Cannot convert date value that is before Unix epoch of 00:00:00 UTC on 1 January 1970.");
}
writer.WriteValue(seconds);
}
/// <summary>
/// Reads the JSON representation of the object.
/// </summary>
/// <param name="reader">The <see cref="JsonReader"/> to read from.</param>
/// <param name="objectType">Type of the object.</param>
/// <param name="existingValue">The existing property value of the JSON that is being converted.</param>
/// <param name="serializer">The calling serializer.</param>
/// <returns>The object value.</returns>
public override object? ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
bool nullable = ReflectionUtils.IsNullable(objectType);
if (reader.TokenType == JsonToken.Null)
{View on GitHub (pinned to 4f73e74372)
Solutions
- Set UnixDateTimeConverter.AllowPreEpoch = true to permit pre-1970 dates.
- If your consumer cannot handle negative epoch values, switch to IsoDateTimeConverter for pre-epoch data.
- Sanitize/clamp dates to the epoch boundary only if pre-epoch values are truly invalid for your domain.
Example fix
// before: default converter rejects pre-epoch dates
var conv = new UnixDateTimeConverter();
settings.Converters.Add(conv);
JsonConvert.SerializeObject(new { Dob = new DateTime(1960,1,1) }, settings);
// after: allow pre-epoch
var conv = new UnixDateTimeConverter { AllowPreEpoch = true };
settings.Converters.Add(conv); Defensive patterns
Strategy: validation
Validate before calling
DateTime value = GetValue();
if (value < new DateTime(1970,1,1) && !converter.AllowPreEpoch)
throw new ArgumentOutOfRangeException(nameof(value), "Date is before Unix epoch; set AllowPreEpoch=true or use IsoDateTimeConverter"); Type guard
static bool IsPostEpoch(DateTime d) => d >= new DateTime(1970, 1, 1);
Try / catch
try { conv.WriteJson(writer, value, serializer); }
catch (JsonSerializationException ex) when (ex.Message.Contains("before Unix epoch"))
{
throw new ArgumentOutOfRangeException("Date predates the Unix epoch; enable AllowPreEpoch", ex);
} Prevention
- Set AllowPreEpoch = true when serializing dates that may predate 1970 (birth dates, history).
- For mixed-era data, consider IsoDateTimeConverter to avoid the epoch boundary entirely.
- Validate date ranges before serialization to surface domain errors early.
When it happens
Trigger: Serializing a DateTime or DateTimeOffset earlier than 1970-01-01T00:00:00Z with UnixDateTimeConverter whose AllowPreEpoch property is false (default). Any birthdate, historical date, or log timestamp before 1970 trips it.
Common situations: Birth dates, historical records, or legacy timestamps before 1970 serialized as Unix seconds. Defaulting AllowPreEpoch to false and forgetting to enable it for data that legitimately predates the epoch.
Related errors
- Expected date object value.
- Unexpected value when converting date. Expected DateTime or
- Expected date object value.
- Namespace attribute must have a value.
- Unexpected value type when writing binary: {0}
AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07).
Data as JSON: /api/errors/f2e7a523c26b70eb.
Report an issue: GitHub.