We have created TypeScript-types for much of the java.time namespace, making it super easy to work with dates and time on the XP-platform! So now you can do this:
import { ZonedDateTime } from "/lib/time";
const date = ZonedDateTime.parse("2023-02-21T12:15:30+01:00");
const fiftyMinutesAgo = date.minusMinutes(50);
const time = fiftyMinutesAgo.toLocalTime();
// time = "11:25:30"
Tip: If you are migrating from one of the npm-libraries to lib-time, you might need to change the formatting strings. There is a difference on how "d" and "D" are interpreted in java.time.format.DateTimeFormatter compared to the npm-libraries.
In version 1.2.1 I have exposed java.util.Locale.LanguageRange, which can be very useful when trying to resolve the best locale for a user – based on the Accept-Language header.
import { LanguageRange, Locale } from "/lib/time";
import { getSupportedLocales } from "/lib/xp/i18n";
import type { Request, Response } from "@enonic-types/core";
export function get(req: Request): Response {
const languageRange = LanguageRange.parse(
req.headers["Accept-Language"]
);
const locale: string = Locale.filterTags(
languageRange,
getSupportedLocales(["i18n/phrases"])
)[0];
...
}