Axios Interceptors & Regional Time Zone Handling: Unresolved Decision on Date Serialization
0 reputation · 11 Apr 2022, 15:13 UTC
0 reputation · 11 Apr 2022, 15:13 UTC
Axios automatically serializes plain JavaScript objects to JSON, but it does not convert Date instances into ISO strings that include a time‑zone offset. When a Date is sent from the browser to a server, the resulting string may lose the local time‑zone context or be interpreted as UTC on the server side.
Using transformRequest and transformResponse interceptors can address this, yet the exact strategy is unclear: should we globally replace every Date with an ISO string, or only target specific fields? Additionally, nested Date values inside arrays or objects add complexity.
Date instances to ISO strings with the original time‑zone offset?Date values be detected and serialized without affecting unrelated string values?Date objects, and how should it handle ambiguous or missing time‑zone data?35275 reputation · 11 Apr 2022, 20:47 UTC
To ensure consistency across regional time zones, you should implement a global request interceptor that recursively converts all Date instances into ISO 8601 strings. This prevents the loss of time-zone context and ensures that the server receives a standardized format regardless of the client's local settings.
Because Axios uses JSON.stringify() internally, Date objects are typically converted to UTC strings. However, to maintain explicit control and handle nested structures, a recursive traversal is required.
Use a recursive function to walk the request payload. This ensures that Date objects nested within arrays or deep objects are caught without altering existing strings.
axios.interceptors.request.use((config) => {
const serializeDates = (data) => {
if (data instanceof Date) return data.toISOString();
if (Array.isArray(data)) return data.map(serializeDates);
if (data !== null && typeof data === 'object') {
return Object.fromEntries(
Object.entries(data).map(([key, value]) => [key, serializeDates(value)])
);
}
return data;
};
if (config.data) {
config.data = serializeDates(config.data);
}
return config;
});
A complementary response interceptor is recommended to restore Date objects. This allows the application logic to use date methods (like .getTime()) immediately without manual parsing in every component.
| Factor | Assumption/Risk | Mitigation |
|---|---|---|
| Performance | Deep traversal may slow down massive payloads. | Limit recursion depth or target specific keys if payloads exceed several MBs. |
| Data Integrity | Some strings may accidentally match the ISO regex. | Use a highly specific regex that requires the full ISO format. |
| Version | Assumes Axios 1.x+ and modern JS environments. | Verify Object.fromEntries support in target browsers. |
Date, receive it back, and verify the final object is an instance of Date with the same millisecond value.Diagnostic Question: Does your API contract require the preservation of the local time-zone offset (e.g., +05:30) rather than converting everything to UTC (Z)? If so, .toISOString() is insufficient, and a library like date-fns or Luxon must be used in the interceptor.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.