A lightweight polyfill for Temporal, successor to the JavaScript Date object
Overall
score
96%
Evaluation — 96%
↑ 1.19xAgent success when using this tile
Utilities for getting current time values in various Temporal formats and time zones. The Temporal.Now namespace provides convenient access to current time information.
The Temporal.Now namespace contains functions for getting the current time in various Temporal formats.
/**
* Utilities for getting current time values
*/
namespace Now {
/** Gets the current system time zone identifier */
function timeZoneId(): string;
/** Gets the current exact moment in time */
function instant(): Instant;
/** Gets the current date and time in the specified time zone with ISO calendar */
function plainDateTimeISO(timeZone?: TimeZoneLike): PlainDateTime;
/** Gets the current date and time with time zone information using ISO calendar */
function zonedDateTimeISO(timeZone?: TimeZoneLike): ZonedDateTime;
/** Gets the current date in the specified time zone with ISO calendar */
function plainDateISO(timeZone?: TimeZoneLike): PlainDate;
/** Gets the current time in the specified time zone with ISO calendar */
function plainTimeISO(timeZone?: TimeZoneLike): PlainTime;
}Retrieve the current system time zone identifier.
/**
* Gets the current system time zone identifier
* @returns The current time zone as an IANA time zone identifier
*/
function timeZoneId(): string;Usage Examples:
import { Temporal } from "temporal-polyfill";
// Get current system time zone
const currentTZ = Temporal.Now.timeZoneId();
console.log(currentTZ); // e.g., "America/New_York", "Europe/London", "UTC"
// Use in other operations
const nowInCurrentTZ = Temporal.Now.zonedDateTimeISO();
const nowInSpecificTZ = Temporal.Now.zonedDateTimeISO(currentTZ);
// Time zone aware operations
function getUserLocalTime(): Temporal.ZonedDateTime {
return Temporal.Now.zonedDateTimeISO(Temporal.Now.timeZoneId());
}Get the current exact moment in time as an Instant.
/**
* Gets the current exact moment in time
* @returns The current instant (Unix timestamp)
*/
function instant(): Instant;Usage Examples:
import { Temporal } from "temporal-polyfill";
// Get current instant
const now = Temporal.Now.instant();
console.log(now.toString()); // ISO string like "2024-03-15T14:30:00.000Z"
// Convert to various formats
const epochMs = now.epochMilliseconds;
const epochNs = now.epochNanoseconds;
// Time measurements and logging
const startTime = Temporal.Now.instant();
// ... perform some operation ...
const endTime = Temporal.Now.instant();
const duration = startTime.until(endTime);
console.log(`Operation took ${duration.total('milliseconds')}ms`);
// Compare with other instants
const futureInstant = now.add({ hours: 1 });
const isNowEarlier = Temporal.Instant.compare(now, futureInstant) < 0; // trueGet the current date and time without time zone information.
/**
* Gets the current date and time in the specified time zone with ISO calendar
* @param timeZone Optional time zone (defaults to system time zone)
* @returns Current date and time without time zone information
*/
function plainDateTimeISO(timeZone?: TimeZoneLike): PlainDateTime;Usage Examples:
import { Temporal } from "temporal-polyfill";
// Get current date-time in system time zone
const now = Temporal.Now.plainDateTimeISO();
console.log(now.toString()); // e.g., "2024-03-15T14:30:00"
// Get current date-time in specific time zone
const nowInNY = Temporal.Now.plainDateTimeISO('America/New_York');
const nowInLondon = Temporal.Now.plainDateTimeISO('Europe/London');
const nowInUTC = Temporal.Now.plainDateTimeISO('UTC');
// Business hours checking
function isBusinessHours(timeZone: string = Temporal.Now.timeZoneId()): boolean {
const now = Temporal.Now.plainDateTimeISO(timeZone);
const hour = now.hour;
const dayOfWeek = now.dayOfWeek;
// Monday-Friday, 9 AM - 5 PM
return dayOfWeek >= 1 && dayOfWeek <= 5 && hour >= 9 && hour < 17;
}
// Scheduling operations
function scheduleForTomorrow(timeZone?: string): Temporal.PlainDateTime {
const now = Temporal.Now.plainDateTimeISO(timeZone);
return now.add({ days: 1 }).with({ hour: 9, minute: 0, second: 0 });
}Get the current date and time with time zone information.
/**
* Gets the current date and time with time zone information using ISO calendar
* @param timeZone Optional time zone (defaults to system time zone)
* @returns Current date and time with time zone information
*/
function zonedDateTimeISO(timeZone?: TimeZoneLike): ZonedDateTime;Usage Examples:
import { Temporal } from "temporal-polyfill";
// Get current zoned date-time in system time zone
const now = Temporal.Now.zonedDateTimeISO();
console.log(now.toString()); // e.g., "2024-03-15T14:30:00-04:00[America/New_York]"
// Get current zoned date-time in specific time zones
const meetings = {
newYork: Temporal.Now.zonedDateTimeISO('America/New_York'),
london: Temporal.Now.zonedDateTimeISO('Europe/London'),
tokyo: Temporal.Now.zonedDateTimeISO('Asia/Tokyo'),
};
// Global scheduling
function getBusinessHoursAcrossTimeZones() {
const timeZones = [
'America/New_York',
'Europe/London',
'Asia/Tokyo'
];
return timeZones.map(tz => ({
timeZone: tz,
now: Temporal.Now.zonedDateTimeISO(tz),
isBusinessHours: isBusinessHours(tz)
}));
}
// Meeting coordination
function findOptimalMeetingTime(timeZones: string[]): Temporal.ZonedDateTime[] {
const baseTime = Temporal.Now.zonedDateTimeISO('UTC').with({ hour: 15, minute: 0 });
return timeZones.map(tz => baseTime.withTimeZone(tz));
}Get the current date without time information.
/**
* Gets the current date in the specified time zone with ISO calendar
* @param timeZone Optional time zone (defaults to system time zone)
* @returns Current date without time information
*/
function plainDateISO(timeZone?: TimeZoneLike): PlainDate;Usage Examples:
import { Temporal } from "temporal-polyfill";
// Get current date
const today = Temporal.Now.plainDateISO();
console.log(today.toString()); // e.g., "2024-03-15"
// Get date in specific time zones
const todayInNY = Temporal.Now.plainDateISO('America/New_York');
const todayInTokyo = Temporal.Now.plainDateISO('Asia/Tokyo');
// Note: dates can differ across time zones due to time differences
const isSameDate = todayInNY.equals(todayInTokyo); // might be false
// Date-based operations
function getDaysUntilWeekend(): number {
const today = Temporal.Now.plainDateISO();
const dayOfWeek = today.dayOfWeek;
if (dayOfWeek === 6 || dayOfWeek === 7) { // Saturday or Sunday
return 0;
}
return 6 - dayOfWeek; // Days until Saturday
}
function getNextBusinessDay(): Temporal.PlainDate {
const today = Temporal.Now.plainDateISO();
const dayOfWeek = today.dayOfWeek;
if (dayOfWeek === 5) { // Friday
return today.add({ days: 3 }); // Monday
} else if (dayOfWeek === 6) { // Saturday
return today.add({ days: 2 }); // Monday
} else if (dayOfWeek === 7) { // Sunday
return today.add({ days: 1 }); // Monday
} else {
return today.add({ days: 1 }); // Next weekday
}
}Get the current time without date information.
/**
* Gets the current time in the specified time zone with ISO calendar
* @param timeZone Optional time zone (defaults to system time zone)
* @returns Current time without date information
*/
function plainTimeISO(timeZone?: TimeZoneLike): PlainTime;Usage Examples:
import { Temporal } from "temporal-polyfill";
// Get current time
const currentTime = Temporal.Now.plainTimeISO();
console.log(currentTime.toString()); // e.g., "14:30:00.123"
// Get time in specific time zones
const timeInNY = Temporal.Now.plainTimeISO('America/New_York');
const timeInLondon = Temporal.Now.plainTimeISO('Europe/London');
const timeInUTC = Temporal.Now.plainTimeISO('UTC');
// Time-based operations
function isAfterHours(): boolean {
const now = Temporal.Now.plainTimeISO();
const closingTime = Temporal.PlainTime.from('17:00:00');
return Temporal.PlainTime.compare(now, closingTime) > 0;
}
function getTimeUntilMeeting(meetingTime: Temporal.PlainTime): Temporal.Duration {
const now = Temporal.Now.plainTimeISO();
return now.until(meetingTime);
}
// Alarm and scheduling
class DailySchedule {
constructor(
public readonly wakeUpTime: Temporal.PlainTime,
public readonly workStartTime: Temporal.PlainTime,
public readonly lunchTime: Temporal.PlainTime,
public readonly workEndTime: Temporal.PlainTime,
public readonly bedTime: Temporal.PlainTime
) {}
getCurrentActivity(): string {
const now = Temporal.Now.plainTimeISO();
if (Temporal.PlainTime.compare(now, this.wakeUpTime) < 0) {
return 'sleeping';
} else if (Temporal.PlainTime.compare(now, this.workStartTime) < 0) {
return 'morning routine';
} else if (Temporal.PlainTime.compare(now, this.lunchTime) < 0) {
return 'working (morning)';
} else if (Temporal.PlainTime.compare(now, this.workEndTime) < 0) {
return 'working (afternoon)';
} else if (Temporal.PlainTime.compare(now, this.bedTime) < 0) {
return 'evening activities';
} else {
return 'sleeping';
}
}
}
const schedule = new DailySchedule(
Temporal.PlainTime.from('07:00:00'),
Temporal.PlainTime.from('09:00:00'),
Temporal.PlainTime.from('12:00:00'),
Temporal.PlainTime.from('17:00:00'),
Temporal.PlainTime.from('22:00:00')
);Working with time zones and current time utilities.
Usage Examples:
import { Temporal } from "temporal-polyfill";
// Multi-timezone dashboard
class WorldClock {
constructor(public readonly timeZones: string[]) {}
getCurrentTimes(): Array<{timeZone: string, time: Temporal.ZonedDateTime}> {
return this.timeZones.map(tz => ({
timeZone: tz,
time: Temporal.Now.zonedDateTimeISO(tz)
}));
}
findBusinessHoursZones(): string[] {
return this.timeZones.filter(tz => {
const now = Temporal.Now.plainTimeISO(tz);
return now.hour >= 9 && now.hour < 17;
});
}
}
// Performance monitoring
class PerformanceTimer {
private startTime: Temporal.Instant | null = null;
start(): void {
this.startTime = Temporal.Now.instant();
}
stop(): Temporal.Duration {
if (!this.startTime) {
throw new Error('Timer not started');
}
const endTime = Temporal.Now.instant();
const duration = this.startTime.until(endTime);
this.startTime = null;
return duration;
}
measure<T>(fn: () => T): { result: T; duration: Temporal.Duration } {
this.start();
const result = fn();
const duration = this.stop();
return { result, duration };
}
}
// System health monitoring
function getSystemStatus() {
const now = Temporal.Now.zonedDateTimeISO();
const uptime = now.since(systemStartTime);
return {
currentTime: now.toString(),
timeZone: now.timeZoneId,
uptime: uptime.toString(),
uptimeDays: uptime.total({ unit: 'day', relativeTo: now }),
systemReady: isSystemReady(now)
};
}// Type definitions for current time utilities
type TimeZoneLike = string | TimeZone;
// All Now functions return current time values in various formats
interface NowFunctions {
timeZoneId(): string;
instant(): Instant;
plainDateTimeISO(timeZone?: TimeZoneLike): PlainDateTime;
zonedDateTimeISO(timeZone?: TimeZoneLike): ZonedDateTime;
plainDateISO(timeZone?: TimeZoneLike): PlainDate;
plainTimeISO(timeZone?: TimeZoneLike): PlainTime;
}
// TimeZone interface (for reference)
interface TimeZone {
readonly id: string;
getOffsetNanosecondsFor(instant: Instant): number;
getOffsetStringFor(instant: Instant): string;
getPlainDateTimeFor(instant: Instant, calendar?: CalendarLike): PlainDateTime;
getInstantFor(dateTime: PlainDateTime, options?: ToInstantOptions): Instant;
getPossibleInstantsFor(dateTime: PlainDateTime): Instant[];
getNextTransition(startingPoint: Instant): Instant | null;
getPreviousTransition(startingPoint: Instant): Instant | null;
toString(): string;
toJSON(): string;
}
interface ToInstantOptions {
disambiguation?: DisambiguationMode;
}
type DisambiguationMode = 'compatible' | 'earlier' | 'later' | 'reject';Install with Tessl CLI
npx tessl i tessl/npm-temporal-polyfilldocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10