In this tutorial, you will learn how to build an end-to-end application using ICU4X to format a date and time with some default locales and additional locales loaded dynamically.
Installing dependencies is always your first step.
We recommend using CodePen to follow along. To load ICU4X into CodePen, you can use this snippet in the JavaScript editor:
import { Locale, DateFormatter, IsoDate, DateTimeLength } from "https://unpkg.com/[email protected]";This loads the full ICU4X WebAssembly file. Since it may take a few seconds to load on slow connections, we'll create a loading div. Add this to your HTML:
<div id="loading">Loading…</div>
<div id="inputoutput" style="display: none">
<p>Ready to rumble!</p>
</div>And in JavaScript, add these lines after the import statement:
document.getElementById("loading").style.display = "none";
document.getElementById("inputoutput").style.display = "block";Here, we will accept a locale string from the user and parse it into an ICU4X Locale.
In the HTML, create an input element for accepting a locale string input, and an output element to echo it back to the user. Add this inside of the inputoutput div:
<!-- inside of div id="inputoutput" -->
<p><label>Locale: <input type="text" id="localeinput" value="en-US"/></label></p>
<p>Output: <output id="output"></output></p>And in JavaScript:
// Create a function that updates the UI:
function update() {
try {
let localeStr = document.getElementById("localeinput").value;
let locale = Locale.fromString(localeStr);
let output = locale.toString();
document.getElementById("output").innerText = output;
} catch(e) {
document.getElementById("output").innerText = e;
}
}
// Run the function whenever the locale input changes:
document.getElementById("localeinput").addEventListener("keyup", update, false);
// Also run the function right now to initialize the UI:
update();Try inputting locales in non-canonical syntax and see them normalized!
Locale: ES-419 Output: es-419
Now we will use built-in locale data to produce a formatted date.
In JavaScript, we will create a datetime input field.
Add this to the HTML:
<!-- inside of div id="inputoutput" -->
<p><label>Date: <input type="date" id="dateinput"/></label></p>And this to JavaScript:
// Run the function whenever the date input changes:
document.getElementById("dateinput").addEventListener("input", update, false);
// Put the following in the update() function, inside the try block:
let dateStr = document.getElementById("dateinput").value;
let dateObj = dateStr ? new Date(dateStr) : new Date();
let isoDate = new IsoDate(dateObj.getFullYear(), dateObj.getMonth() + 1, dateObj.getDate());
let dateFormatter = DateFormatter.createYmd(locale, DateTimeLength.Long);
let output = dateFormatter.formatIso(isoDate);
document.getElementById("output").innerText = output;Try this in several locales, like en (English), en-GB (British English), and th (Thai). Observe how differently dates are represented in locales around the world! You can explicitly specify arbitrary calendar systems using the u-ca Unicode extension keyword in the locale. Try en-u-ca-hebrew!
Now we would also like to format the current time.
Use the API documentation for Time and DateTimeFormatter to expand your app to format both a date and a time.
Hint: You can create an HTML time picker with
<p><label>Time: <input type="time" id="timeinput" value="10:10"/></label></p>Hint: You can create a Date from dateStr and timeStr with
let dateObj = dateStr && timeStr ? new Date(dateStr + " " + timeStr) : new Date();Note that Dates constructed this way will be in UTC.