Skip to content

parseHttpDate

parseHttpDate(value: string): string

Parse an HTTP header date — the RFC 9110 HTTP-date that headers like Last-Modified/Date/Expires carry — into a UTC ISO 8601 datetime string.

  • Decoding, not display. Accepts English weekday/month names only, per the fixed grammar (see formatHttpDate’s JSDoc).
  • All three HTTP-date formats, as RFC 9110 §5.6.7 requires (“A recipient that parses a timestamp value in an HTTP field MUST accept all three”): IMF-fixdate (Sun, 06 Nov 1994 08:49:37 GMT), the obsolete rfc850-date (Sunday, 06-Nov-94 08:49:37 GMT) and asctime-date (Sun Nov 6 08:49:37 1994, read as UTC).
  • HTTP-date is case-sensitive and fixed-width: exactly the spacing, digit counts and the literal GMT of the grammar. No numeric offset is accepted.
  • rfc850-date years read the clock. Its two-digit year becomes the latest year ending in those digits whose timestamp is not more than 50 years after now (Temporal.Now.instant()), per RFC 9110: “a timestamp that appears to be more than 50 years in the future” is “the most recent year in the past that had the same last two digits”. The same string can therefore parse differently decades apart; if the clock cannot be read the result is "".
  • The day name must match the date, in all three formats. RFC 9110 gives it RFC 5322’s semantics: “the day-of-week (if included) MUST be the day implied by the date”. A mismatch returns "" — deliberately in asctime-date too, where the day name is not separated by a comma and so is easy to overlook. Mon Nov 6 08:49:37 1994 is "", exactly as Mon, 06 Nov 1994 08:49:37 GMT and Monday, 06-Nov-94 08:49:37 GMT are: 6 November 1994 was a Sunday. A parser that accepted the mismatch would have to pick which of the two fields to believe, and there is no rule in RFC 9110 that says which.
  • An impossible calendar date (31 Feb, 29 Feb 2023) returns "": a parser never invents a date, so fields are validated with overflow: "reject", not clamped.
  • For the IMF-fixdate shape alone, test the exported httpDate pattern.
  • Compatibility: before 1.16.0 a day name that did not match the date was ignored. Parse the same fields as an RFC 5322 date-time without the day name, then convert.
ParameterTypeDescription
valuestringRFC 9110 HTTP-date string (e.g. “Fri, 15 Mar 2024 14:30:00 GMT”)

UTC ISO 8601 datetime string, or "" on invalid input

import { parseHttpDate } from "@northguild/gmt/utc/parse";
parseHttpDate("Fri, 15 Mar 2024 14:30:00 GMT") // "2024-03-15T14:30:00Z"
output 

utc/parse/parseHttpDate.ts