Mit JavaScript-Datumsobjekten können wir mit Datumsangaben arbeiten:
Ein Beispiel für das Abrufen des aktuellen Jahres in Javascript:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getFullYear() Method</h2>
<p>Return the full year of a date object:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getFullYear();
</script>
</body>
</html>
Ein Beispiel für das Abrufen des aktuellen Monats in Javascript:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMonth() Method</h2>
<p>Return the month of a date as a number from 0 to 11.</p>
<p>To get the correct month number, you must add 1:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getMonth() + 1;
</script>
</body>
</html>
Ein Beispiel für das Abrufen des aktuellen Tages in Javascript:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDate() Method</h2>
<p>Return the day of a date as a number (1-31):</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getDate();
</script>
</body>
</html>
Ein Beispiel für das Abrufen der aktuellen Stunde in Javascript:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getHours() Method</h2>
<p>Return the hours of a date as a number (0-23):</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getHours();
</script>
</body>
</html>
Ein Beispiel für das Abrufen der aktuellen Minute in Javascript:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMinutes() Method</h2>
<p>Returns the minutes of a date as a number (0-59):</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getMinutes();
</script>
</body>
</html>
Ein Beispiel für das Abrufen der aktuellen Sekunde in Javascript:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getSeconds() Method</h2>
<p>Return the seconds of a date as a number (0-59):</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getSeconds();
</script>
</body>
</html>
const d = new Date();
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p>new Date() without arguments, creates a date object with the current date and time:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
const d = new Date("2022-03-25");
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p id="demo"></p>
<script>
const d = new Date("2022-03-25");
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
Datumsobjekte sind statisch. Die „Uhr“ „läuft“ nicht.
Die Uhr des Computers tickt, Datumsobjekte jedoch nicht.
Standardmäßig verwendet JavaScript die Zeitzone des Browsers und zeigt ein Datum als Volltextzeichenfolge an:
Später in diesem Tutorial erfahren Sie viel mehr über die Anzeige von Datumsangaben.
Datumsobjekte werden mit erstellt new Date()
-Konstruktor.
Es gibt 9 Möglichkeiten, ein neues Datumsobjekt zu erstellen:
new Date()
new Date(date string)
new Date(year,month)
new Date(year,month,day)
new Date(year,month,day,hours)
new Date(year,month,day,hours,minutes)
new Date(year,month,day,hours,minutes,seconds)
new Date(year,month,day,hours,minutes,seconds,ms)
new Date(milliseconds)
new Date()
new Date()
erstellt ein Datumsobjekt mit dem aktuellen Datum und der aktuellen Uhrzeit:
const d = new Date();
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p>Create a new date object with the current date and time:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
neues Datum(Datumszeichenfolge)
new Date(date string)
erstellt ein Datumsobjekt aus einer date string:
const d = new Date("October 13, 2014 11:13:00");
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p>A date object can be created with a specified date and time:</p>
<p id="demo"></p>
<script>
const d = new Date("October 13, 2014 11:13:00");
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
const d = new Date("2022-03-25");
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p id="demo"></p>
<script>
const d = new Date("2022-03-25");
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
Datumszeichenfolgenformate werden im nächsten Kapitel beschrieben.
neues Datum(Jahr, Monat, ...)
new Date(Jahr, Monat, ...)
erstellt ein Datumsobjekt mit einem angegebenen Datum und einer angegebenen Uhrzeit.
7 Zahlen geben Jahr, Monat, Tag, Stunde, Minute, Sekunde und Millisekunde an (in dieser Reihenfolge):
const d = new Date(2018, 11, 24, 10, 33, 30, 0);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>Using new Date(7 numbers), creates a new date object with the specified date and time:</p>
<p id="demo"></p>
<script>
const d = new Date(2018, 11, 24, 10, 33, 30, 0);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
JavaScript zählt Monate von 0 bis 11:
Januar=0.
Dezember=11.
Die Angabe eines Monats über 11 führt nicht zu einem Fehler, sondern fügt den Überlauf zum nächsten Jahr hinzu:
Angabe:
const d = new Date(2018, 15, 24, 10, 33, 30);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript new Date()</h1>
<p>JavaScript counts months from 0 to 11.</p>
<p>Specifying a month higher than 11, will not result in an error but add the overflow to the next year:</p>
<p id="demo"></p>
<script>
const d = new Date(2018, 15, 24, 10, 33, 30, 0);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
Ist das gleiche wie:
const d = new Date(2019, 3, 24, 10, 33, 30);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript new Date()</h1>
<p>JavaScript counts months from 0 to 11.</p>
<p id="demo"></p>
<script>
const d = new Date(2019, 3, 24, 10, 33, 30, 0);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
Die Angabe eines Tages, der höher als max. ist, führt nicht zu einem Fehler, fügt aber den Überlauf zum nächsten Monat hinzu:
Angabe:
const d = new Date(2018, 5, 35, 10, 33, 30);
Ist das gleiche wie:
const d = new Date(2018, 6, 5, 10, 33, 30);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>JavaScript counts months from 0 to 11.</p>
<p>Specifying a day higher than max, will not result in an error but add the overflow to the next month:</p>
<p id="demo"></p>
<script>
const d = new Date(2018, 05, 35, 10, 33, 30, 0);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
6 Zahlen geben Jahr, Monat, Tag, Stunde, Minute, Sekunde an:
const d = new Date(2018, 11, 24, 10, 33, 30);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>6 numbers specify year, month, day, hour, minute and second:</p>
<p id="demo"></p>
<script>
const d = new Date(2018, 11, 24, 10, 33, 30);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
5 Zahlen geben Jahr, Monat, Tag, Stunde und Minute an:
const d = new Date(2018, 11, 24, 10, 33);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>5 numbers specify year, month, day, hour, and minute:</p>
<p id="demo"></p>
<script>
const d = new Date(2018, 11, 24, 10, 33);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
4 Zahlen geben Jahr, Monat, Tag und Stunde an:
const d = new Date(2018, 11, 24, 10);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>4 numbers specify year, month, day, and hour:</p>
<p id="demo"></p>
<script>
const d = new Date(2018, 11, 24, 10);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
3 Zahlen geben Jahr, Monat und Tag an:
const d = new Date(2018, 11, 24);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>3 numbers specify year, month, and day:</p>
<p id="demo"></p>
<script>
const d = new Date(2018, 11, 24);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
2 Zahlen geben Jahr und Monat an:
const d = new Date(2018, 11);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>2 numbers specify year and month:</p>
<p id="demo"></p>
<script>
const d = new Date(2018, 11);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
Sie können den Monat nicht weglassen. Wenn Sie nur einen Parameter angeben, wird dieser als Millisekunden behandelt.
const d = new Date(2018);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>One parameter will be interpreted as new Date(milliseconds).</p>
<p id="demo"></p>
<script>
const d = new Date(2018);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
Ein- und zweistellige Jahreszahlen werden als 19xx interpretiert:
const d = new Date(99, 11, 24);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>Two digit years will be interpreted as 19xx:</p>
<p id="demo"></p>
<script>
const d = new Date(99, 11, 24);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
const d = new Date(9, 11, 24);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h2>JavaScript new Date()</h2>
<p>One digit years will be interpreted as 19xx:</p>
<p id="demo"></p>
<script>
const d = new Date(9, 11, 24);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
JavaScript speichert Daten als Anzahl der Millisekunden seit dem 1. Januar 1970.
Nullzeit ist der 1. Januar 1970 00:00:00 UTC.
Ein Tag (24 Stunden) beträgt 86.400.000 Millisekunden.
Jetzt ist es soweit:
Millisekunden nach dem 1. Januar 1970
neues Datum (Millisekunden)
new Date(milliseconds)
erstellt ein neues Datumsobjekt als milliseconds plus Nullzeit:
01. Januar 1970 plus 100.000.000.000 Millisekunden ist:
const d = new Date(100000000000);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p>100000000000 milliseconds from January 01 1970 UTC is:</p>
<p id="demo"></p>
<script>
const d = new Date(100000000000);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
1. Januar 1970 minus 100.000.000.000 Millisekunden ist:
const d = new Date(-100000000000);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p>-100000000000 milliseconds from January 01 1970 UTC is:</p>
<p id="demo"></p>
<script>
const d = new Date(-100000000000);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
1. Januar 1970 plus 24 Stunden ist:
const d = new Date(24 * 60 * 60 * 1000);
// or
const d = new Date(86400000);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p>86400000 milliseconds from January 01 1970 UTC is:</p>
<p id="demo"></p>
<script>
const d = new Date(86400000);
document.getElementById("demo").innerHTML = d;
</script>
<p>One day (24 hours) is 86,400,000 milliseconds.</p>
</body>
</html>
01. Januar 1970 plus 0 Millisekunden ist:
const d = new Date(0);
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p>0 milliseconds from January 01 1970 UTC is:</p>
<p id="demo"></p>
<script>
const d = new Date(0);
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
Wenn ein Datumsobjekt erstellt wird, können Sie es mit einer Reihe von Methoden bearbeiten Es.
Mit Datumsmethoden können Sie Jahr, Monat, Tag, Stunde usw. abrufen und festlegen. Minute, Sekunde und Millisekunde von Datumsobjekten, entweder Ortszeit oder UTC (universelle oder GMT-)Zeit.
Datumsmethoden und Zeitzonen werden in den nächsten Kapiteln behandelt.
JavaScript gibt Datumsangaben (standardmäßig) mit der Methode toString() aus. Dies ist eine Zeichenfolgendarstellung des Datums, einschließlich der Zeitzone. Das Format ist in der ECMAScript-Spezifikation angegeben:
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using new Date()</h2>
<p>new Date() without arguments, creates a date object with the current date and time:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d;
</script>
</body>
</html>
Wenn Sie ein Datumsobjekt in HTML anzeigen, wird es automatisch in ein konvertiert string, mit der Methode toString()
.
const d = new Date();
d.toString();
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The toString() Method</h2>
<p>Convert a date to a string:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.toString();
</script>
</body>
</html>
Die Methode toDateString()
wandelt ein Datum in ein besser lesbares Datum um Format:
const d = new Date();
d.toDateString();
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The toDateString() Method</h2>
<p>Convert a date to a date string:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.toDateString();
</script>
</body>
</html>
Die Methode toUTCString()
wandelt ein Datum mithilfe des UTC-Standards in eine Zeichenfolge um:
const d = new Date();
d.toUTCString();
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The toUTCString() Method</h2>
<p>Convert a date to a string using the UTC standard:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.toUTCString();
</script>
</body>
</html>
Die Methode toISOString()
wandelt ein Datum mithilfe des ISO-Standards in eine Zeichenfolge um:
const d = new Date();
d.toISOString();
Probieren Sie es selbst aus →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The toISOString() Method</h2>
<p>Convert a date to a string using the ISO standard:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.toISOString();
</script>
</body>
</html>
Eine vollständige Datumsreferenz finden Sie auf unserer:
Vollständige JavaScript-Datumsreferenz.
Die Referenz enthält Beschreibungen und Beispiele aller Datumseigenschaften und Methoden.