API für die aktuelle Uhrzeit?

2 Antworten

TimeAPI ist eine kostenlose und leicht zu verwendende API für die aktuelle Uhrzeit. Über fetch() kannst Du die API in JavaScript abrufen:

var url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam";

fetch(url)
    .then(response => response.json())
    .then(response => {
        var dateTime = new Date(response.dateTime);
    });

Alternativ ginge es auch mit einem XMLHttpRequest (XHR):

var xhr = new XMLHttpRequest();
var url = "https://timeapi.io/api/Time/current/zone?timeZone=Europe/Amsterdam";

xhr.onreadystatechange = function() {
    if(this.readyState == 4 && this.status == 200) {
        var response = JSON.parse(this.responseText);
        var dateTime = new Date(response.dateTime);
        console.log(dateTime);
    }
};
xhr.open("GET", url, true);
xhr.send();
Woher ich das weiß:Recherche