API Demo

Explore WorldTimeApp API interfaces and learn how to integrate timezone data into your applications

Cities Data API

Get All Cities

GET /api/cities

Get City Details

GET /api/cities/[id]

Search Cities by Name

GET /api/cities/name/[name]

Response Example

{
  "id": "tokyo",
  "city": "东京",
  "cityEn": "Tokyo",
  "country": "日本",
  "countryEn": "Japan",
  "flag": "🇯🇵",
  "timezone": "Asia/Tokyo",
  "utcOffset": "UTC+9",
  "continent": "亚洲",
  "continentEn": "Asia",
  "latitude": 35.6762,
  "longitude": 139.6503,
  "population": 13960000,
  "currency": "JPY",
  "language": "Japanese"
}

JavaScript Example

// Fetch city data
async function getCityTime(cityId) {
  try {
    const response = await fetch(`/api/cities/${cityId}`);
    const city = await response.json();
    
    // Get current time
    const now = new Date();
    const formatter = new Intl.DateTimeFormat('en-US', {
      timeZone: city.timezone,
      hour12: false,
      hour: '2-digit',
      minute: '2-digit',
      second: '2-digit'
    });
    
    const time = formatter.format(now);
    console.log(`${city.cityEn}: ${time}`);
    
    return { city, time };
  } catch (error) {
    console.error('Error fetching city data:', error);
  }
}

// Usage example
getCityTime('tokyo');
getCityTime('new-york');
getCityTime('london');

Python Example

import requests
from datetime import datetime
import pytz

def get_city_time(city_id):
    """Get city time information"""
    try:
        # Fetch city data
        response = requests.get(f'https://worldtimeapp.online/api/cities/{city_id}')
        city = response.json()
        
        # Get timezone info
        timezone = pytz.timezone(city['timezone'])
        current_time = datetime.now(timezone)
        
        print(f"{city['cityEn']}: {current_time.strftime('%H:%M:%S')}")
        
        return {
            'city': city,
            'time': current_time.strftime('%H:%M:%S'),
            'date': current_time.strftime('%Y-%m-%d')
        }
    except Exception as e:
        print(f"Error: {e}")
        return None

# Usage example
get_city_time('tokyo')
get_city_time('new-york')
get_city_time('london')

React Component Example

import React, { useState, useEffect } from 'react';

function WorldClock({ cityId }) {
  const [cityData, setCityData] = useState(null);
  const [currentTime, setCurrentTime] = useState('');

  useEffect(() => {
    // Fetch city data
    fetch(`/api/cities/${cityId}`)
      .then(response => response.json())
      .then(data => setCityData(data))
      .catch(error => console.error('Error:', error));
  }, [cityId]);

  useEffect(() => {
    if (!cityData) return;

    // Update time
    const updateTime = () => {
      const now = new Date();
      const formatter = new Intl.DateTimeFormat('en-US', {
        timeZone: cityData.timezone,
        hour12: false,
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit'
      });
      setCurrentTime(formatter.format(now));
    };

    updateTime();
    const interval = setInterval(updateTime, 1000);

    return () => clearInterval(interval);
  }, [cityData]);

  if (!cityData) return <div>Loading...</div>;

  return (
    <div className="city-clock">
      <h3>{cityData.cityEn}</h3>
      <div className="time">{currentTime}</div>
      <div className="timezone">{cityData.timezone}</div>
      <div className="country">{cityData.countryEn}</div>
    </div>
  );
}

export default WorldClock;

API Documentation

Base URL

https://worldtimeapp.online/api

Supported Formats

JSON

Rate Limits

Maximum 60 requests per minute

Error Handling

API returns standard HTTP status codes. 404 for city not found, 500 for server errors.

Get Started

Start using our API today to add global timezone functionality to your applications

View Live Demo