Skip to content

Main content

World Clock Source Code

Browse the implementation files for World Clock.
tool.ts typescript
// Tool: World Clock

type ClockFormat = '12-hour' | '24-hour';

const FORMATS: ClockFormat[] = ['24-hour', '12-hour'];
const LOCATIONS = [
	{ name: 'UTC', timeZone: 'UTC' },
	{ name: 'Los Angeles', timeZone: 'America/Los_Angeles' },
	{ name: 'New York', timeZone: 'America/New_York' },
	{ name: 'São Paulo', timeZone: 'America/Sao_Paulo' },
	{ name: 'London', timeZone: 'Europe/London' },
	{ name: 'Berlin', timeZone: 'Europe/Berlin' },
	{ name: 'Johannesburg', timeZone: 'Africa/Johannesburg' },
	{ name: 'Dubai', timeZone: 'Asia/Dubai' },
	{ name: 'New Delhi', timeZone: 'Asia/Kolkata' },
	{ name: 'Singapore', timeZone: 'Asia/Singapore' },
	{ name: 'Tokyo', timeZone: 'Asia/Tokyo' },
	{ name: 'Sydney', timeZone: 'Australia/Sydney' }
] as const;

const parseDate = (input: unknown): Date | null => {
	if (input === undefined) return new Date();

	const date = input instanceof Date ? input : new Date(input as string | number);

	return Number.isNaN(date.getTime()) ? null : date;
};

const getPart = (
	parts: Intl.DateTimeFormatPart[],
	type: Intl.DateTimeFormatPartTypes
): string => parts.find((part) => part.type === type)?.value ?? '';

const formatAtLocation = (
	date: Date,
	timeZone: string,
	format: ClockFormat
): string => {
	const parts = new Intl.DateTimeFormat('en-US', {
		timeZone,
		year: 'numeric',
		month: '2-digit',
		day: '2-digit',
		hour: '2-digit',
		minute: '2-digit',
		second: '2-digit',
		hourCycle: format === '24-hour' ? 'h23' : 'h12'
	}).formatToParts(date);
	const datePart = `${getPart(parts, 'year')}-${getPart(parts, 'month')}-${getPart(parts, 'day')}`;
	const timePart = `${getPart(parts, 'hour')}:${getPart(parts, 'minute')}:${getPart(parts, 'second')}`;

	return format === '12-hour'
		? `${datePart} ${timePart} ${getPart(parts, 'dayPeriod')}`
		: `${datePart} ${timePart}`;
};

export const worldClock = (
	formatInput: unknown,
	dateInput?: unknown
): string => {
	if (!FORMATS.includes(formatInput as ClockFormat)) return '';

	const date = parseDate(dateInput);
	if (date === null) return '';

	try {
		return LOCATIONS.map(
			({ name, timeZone }) =>
				`${name}: ${formatAtLocation(date, timeZone, formatInput as ClockFormat)}`
		).join('\n');
	} catch {
		return '';
	}
};

export const Tool: TypeTool = {
	name: 'World Clock',
	slug: 'world-clock',
	intro: 'Compare the current date and time across major world cities.',
	tool: worldClock,
	i: [
		{
			example: '24-hour',
			label: 'Clock Format',
			placeholder: 'Select a clock format',
			type: 'select',
			value: '24-hour',
			options: [
				{ value: '24-hour', text: '24-hour', selected: true },
				{ value: '12-hour', text: '12-hour' }
			]
		}
	],
	o: [
		{
			example: 'UTC: 2026-07-22 16:00:00',
			label: 'World Times',
			placeholder: 'World times will appear here...',
			render: 'textarea',
			type: 'text'
		}
	],
	rerunnable: true,
	alternative: [{ title: 'Clock', url: '/clock' }],
	cite: [
		{
			title: 'World clock - Wikipedia',
			url: 'https://en.wikipedia.org/wiki/World_clock'
		},
		{
			title: 'List of tz database time zones - Wikipedia',
			url: 'https://en.wikipedia.org/wiki/List_of_tz_database_time_zones'
		}
	]
};

export default Tool;

Description

Browse the implementation files for World Clock.

References

  1. Tool.io on GitHub
  2. Tool.io TypeScript tools library

Similar or alternative tools