To convert a UTC ISO date string to an ISO string in the "Europe/Paris" timezone, you can use JavaScript's built-in Date object along with the toLocaleString method or a library like date-fns-tz for more robust timezone handling. Below are two approaches:
Approach 1: Using Native JavaScript (with Intl.DateTimeFormat)
JavaScript's Intl.DateTimeFormat API allows you to format dates in specific timezones. Here's how you can do it:
function convertUTCToParis(utcISOString) {
// Create a Date object from the UTC ISO string
const utcDate = new Date(utcISOString);
// Define options for formatting the date in Europe/Paris timezone
const options = {
timeZone: "Europe/Paris",
year: "numeric",
month: "2-digit",
day: "2-digit",
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
fractionalSecondDigits: 3, // For milliseconds
hour12: false, // Use 24-hour format
};
// Format the date to Europe/Paris timezone
const parisDateString = utcDate.toLocaleString("sv-SE", options)
.replace(" ", "T") // Replace space with 'T' to match ISO format
.replace(/(\d+)-(\d+)-(\d+)/, "$3-$2-$1"); // Reorder to YYYY-MM-DD
return parisDateString;
}
// Example usage
const utcISOString = "2023-10-15T14:30:00.000Z";
const parisISOString = convertUTCToParis(utcISOString);
console.log(parisISOString); // Output: "2023-10-15T16:30:00.000" (Paris is UTC+2 during DST)
Notes:
- The
toLocaleString method with sv-SE locale is used because it produces a format close to ISO (YYYY-MM-DD HH:mm:ss).
- This approach does not append the timezone offset (e.g.,
+02:00) to the ISO string. If you need the offset, consider using a library (see Approach 2).
- Be aware of Daylight Saving Time (DST) in Europe/Paris, which changes the offset between UTC+1 and UTC+2 depending on the time of year.
Approach 2: Using date-fns-tz (Recommended for Robustness)
For more reliable and flexible timezone handling, you can use the date-fns-tz library, which is an extension of the popular date-fns library. This approach is recommended for production applications because it handles edge cases (like DST) more effectively.
First, install the library:
npm install date-fns date-fns-tz
Then, use the following code:
const { zonedTimeToUtc, utcToZonedTime, format } = require("date-fns-tz");
// Function to convert UTC ISO string to Europe/Paris ISO string
function convertUTCToParis(utcISOString) {
// Parse the UTC ISO string to a Date object
const utcDate = new Date(utcISOString);
// Convert the UTC date to Europe/Paris timezone
const parisDate = utcToZonedTime(utcDate, "Europe/Paris");
// Format the date as an ISO string (without timezone suffix)
const parisISOString = format(parisDate, "yyyy-MM-dd'T'HH:mm:ss.SSS", {
timeZone: "Europe/Paris",
});
return parisISOString;
}
// Example usage
const utcISOString = "2023-10-15T14:30:00.000Z";
const parisISOString = convertUTCToParis(utcISOString);
console.log(parisISOString); // Output: "2023-10-15T16:30:00.000" (Paris is UTC+2 during DST)
Notes:
- The
date-fns-tz library uses the IANA timezone database (e.g., "Europe/Paris") and handles DST transitions automatically.
- If you want the ISO string to include the timezone offset (e.g.,
+02:00), you can modify the format string to yyyy-MM-dd'T'HH:mm:ss.SSSXXX.
Key Considerations
- DST Handling: Europe/Paris uses UTC+1 in standard time and UTC+2 during DST. Both approaches handle this automatically, but
date-fns-tz is more reliable for edge cases.
- Browser Compatibility: The
Intl.DateTimeFormat API (Approach 1) is widely supported in modern browsers, but older browsers may not support it. For broader compatibility, consider using a library like date-fns-tz.
- ISO Format: If you need the full ISO format with timezone offset (e.g.,
2023-10-15T16:30:00.000+02:00), use date-fns-tz with the appropriate format string.
Recommendation
For simple use cases, Approach 1 (native JavaScript) is sufficient. For production applications or complex timezone handling, use Approach 2 (date-fns-tz) to ensure accuracy and maintainability.