/** * Builds a proper UTC HttpDate timestamp from a Date object * since not all environments will have this as the expected * format. * * See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toUTCString * > Prior to ECMAScript 2018, the format of the return value * > varied according to the platform. The most common return * > value was an RFC-1123 formatted date stamp, which is a * > slightly updated version of RFC-822 date stamps. */ // Build indexes outside so we allocate them once. const days: Array = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]; // prettier-ignore const months: Array = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; export function dateToUtcString(date: Date): string { const year = date.getUTCFullYear(); const month = date.getUTCMonth(); const dayOfWeek = date.getUTCDay(); const dayOfMonthInt = date.getUTCDate(); const hoursInt = date.getUTCHours(); const minutesInt = date.getUTCMinutes(); const secondsInt = date.getUTCSeconds(); // Build 0 prefixed strings for contents that need to be // two digits and where we get an integer back. const dayOfMonthString = dayOfMonthInt < 10 ? `0${dayOfMonthInt}` : `${dayOfMonthInt}`; const hoursString = hoursInt < 10 ? `0${hoursInt}` : `${hoursInt}`; const minutesString = minutesInt < 10 ? `0${minutesInt}` : `${minutesInt}`; const secondsString = secondsInt < 10 ? `0${secondsInt}` : `${secondsInt}`; return `${days[dayOfWeek]}, ${dayOfMonthString} ${months[month]} ${year} ${hoursString}:${minutesString}:${secondsString} GMT`; }