Keywords: JavaScript | ISO8601 | date parsing | formatting | regular expressions
Abstract: This article provides a detailed guide on parsing and formatting ISO 8601 dates in JavaScript. It covers native Date object support, custom formatting with regular expressions, timezone handling, and alternative approaches, aiming for clean and minimal solutions.
Introduction
ISO 8601 is an international standard for date and time representations. In JavaScript, parsing such dates is essential for many applications. This article explores efficient methods to parse and format ISO 8601 dates, focusing on clean and minimal solutions.
JavaScript Date Object and ISO 8601 Support
The JavaScript Date object natively supports ISO 8601 format when passed as a string parameter. For example:
var date = new Date("2014-04-07T13:58:10.104Z");
console.log(date.toString());
This method handles various ISO 8601 variants, including timezone offsets.
Custom Formatting Using Regular Expressions
To achieve a specific format like "January 28, 2011 - 7:30PM EST", regular expressions can be used. Based on the best answer, here's a refined approach:
// Example input: "2011-01-28T19:30:00EST"
const months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
const formattedDate = input.replace(
/(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):\d{2}(\w{3})/,
function(match, year, month, day, hour, minute, timezone) {
const monthName = months[parseInt(month) - 1];
const hour12 = hour % 12 || 12; // Handle 12-hour format
const ampm = hour >= 12 ? "PM" : "AM";
return `${monthName} ${day}, ${year} - ${hour12}:${minute}${ampm} ${timezone}`;
}
);
console.log(formattedDate); // Output: January 28, 2011 - 7:30PM EST
Handling Timezone Variations
For timezones like "EST" or offsets like "-05:00", the Date object can be leveraged. A robust method is:
const input = "2011-01-28T19:30:00-05:00";
const date = new Date(input);
const dateString = date.toString(); // e.g., "Fri Jan 28 2011 19:30:00 GMT-0500 (EST)"
// Use regex to extract and format
const monthsAbbr = {Jan: "January", Feb: "February", Mar: "March", Apr: "April", May: "May", Jun: "June", Jul: "July", Aug: "August", Sep: "September", Oct: "October", Nov: "November", Dec: "December"};
const formatted = dateString.replace(
/\w{3} (\w{3}) (\d{2}) (\d{4}) (\d{2}):(\d{2}):[^(]+\(([A-Z]{3})\)/,
function(match, monthAbbr, day, year, hour, minute, timezone) {
const monthName = monthsAbbr[monthAbbr];
const hour12 = hour % 12 || 12;
const ampm = hour >= 12 ? "PM" : "AM";
return `${monthName} ${day}, ${year} - ${hour12}:${minute}${ampm} ${timezone}`;
}
);
console.log(formatted); // January 28, 2011 - 7:30PM EST
Alternative Approaches and Libraries
Libraries like datejs can simplify parsing. For instance:
Date.parse('1997-07-16T19:20:15'); // ISO 8601 format
However, for minimal solutions, native JavaScript methods are preferred.
Conclusion
Parsing and formatting ISO 8601 dates in JavaScript can be efficiently handled using the Date object combined with custom logic. This approach ensures clean and maintainable code, suitable for various applications.