new Date()
-konstruktørenI JavaScript oprettes datoobjekter med new Date()
.
new Date()
returnerer et datoobjekt med den aktuelle dato og klokkeslæt.
const date = new Date();
Prøv det selv →
<!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>
Få år som et firecifret tal (åååå)
Få måned som et tal (0-11)
Få dag som et tal (1-31)
Få ugedag som et tal (0-6)
Få time (0-23)
Få minut (0-59)
Få sekund (0-59)
Få millisekunder (0-999)
Få tid (millisekunder siden 1. januar 1970)
Get-metoderne ovenfor returnerer Lokal tid.
Universal tid (UTC) er dokumenteret nederst på denne side.
Get-metoderne returnerer information fra eksisterende datoobjekter.
I et datoobjekt er tiden statisk. "Uret" "kører" ikke.
Tiden i et datoobjekt er IKKE det samme som det aktuelle klokkeslæt.
getFullYear()
Metoden getFullYear()
returnerer året for en dato som et firecifret tal:
const d = new Date("2021-03-25");
d.getFullYear();
Prøv det selv →
<!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("2021-03-25")
document.getElementById("demo").innerHTML = d.getFullYear();
</script>
</body>
</html>
const d = new Date();
d.getFullYear();
Prøv det selv →
<!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>
Gammel JavaScript-kode bruger muligvis ikke-standardmetoden getYear().
getYear() formodes at returnere et 2-cifret årstal.
getYear() er forældet. Brug det ikke!
getMonth()
Metoden getMonth()
returnerer måneden for en dato som et tal (0-11).
I JavaScript er januar måned nummer 0, februar er nummer 1, ...
Endelig er december måned nummer 11.
const d = new Date("2021-03-25");
d.getMonth();
Prøv det selv →
<!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("2021-03-25");
document.getElementById("demo").innerHTML = d.getMonth() + 1;
</script>
</body>
</html>
const d = new Date();
d.getMonth();
Prøv det selv →
<!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>
Du kan bruge en række navne til at returnere måneden som et navn:
const months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];
const d = new Date("2021-03-25");
let month = months[d.getMonth()];
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>JavaScript getMonth()</h2>
<p>Return the month as a number.</p>
<p>You can use an array of names to return the month as a name:</p>
<p id="demo"></p>
<script>
const months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
const d = new Date("2021-03-25");
let month = months[d.getMonth()];
document.getElementById("demo").innerHTML = month;
</script>
</body>
</html>
const months = ["January", "February", "March", "April", "May",
"June", "July", "August", "September", "October",
"November", "December"];
const d = new Date();
let month = months[d.getMonth()];
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>JavaScript getMonth()</h2>
<p>Return the month as a number.</p>
<p>You can use an array of names to return the month as a name:</p>
<p id="demo"></p>
<script>
const months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
const d = new Date();
let month = months[d.getMonth()];
document.getElementById("demo").innerHTML = month;
</script>
</body>
</html>
getDate()
Metoden getDate()
returnerer dagen for en dato som et tal (1-31):
const d = new Date("2021-03-25");
d.getDate();
Prøv det selv →
<!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("2021-03-25");
document.getElementById("demo").innerHTML = d.getDate();
</script>
</body>
</html>
const d = new Date();
d.getDate();
Prøv det selv →
<!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>
getHours()
Metoden getHours()
returnerer timerne på en dato som et tal (0-23):
const d = new Date("2021-03-25");
d.getHours();
Prøv det selv →
<!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("2021-03-25");document.getElementById("demo").innerHTML = d.getHours();
</script>
</body>
</html>
const d = new Date();
d.getHours();
Prøv det selv →
<!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>
getMinutes()
Metoden getMinutes()
returnerer minutterne for en dato som et tal (0-59):
const d = new Date("2021-03-25");
d.getMinutes();
Prøv det selv →
<!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("2021-03-25");
document.getElementById("demo").innerHTML = d.getMinutes();
</script>
</body>
</html>
const d = new Date();
d.getMinutes();
Prøv det selv →
<!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>
getSeconds()
Metoden getSeconds()
returnerer sekunderne af en dato som et tal (0-59):
const d = new Date("2021-03-25");
d.getSeconds();
Prøv det selv →
<!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("2021-03-25");
document.getElementById("demo").innerHTML = d.getSeconds();
</script>
</body>
</html>
const d = new Date();
d.getSeconds();
Prøv det selv →
<!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>
getMilliseconds()
Metoden getMilliseconds()
returnerer millisekunderne af en dato som et tal (0-999):
const d = new Date("2021-03-25");
d.getMilliseconds();
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMilliseconds() Method</h2>
<p>Return the milliseconds of a date as a number (0-999):</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getMilliseconds();
</script>
</body>
</html>
const d = new Date();
d.getMilliseconds();
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getMilliseconds() Method</h2>
<p>Return the milliseconds of a date as a number (0-999):</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getMilliseconds();
</script>
</body>
</html>
getDay()
Metoden getDay()
returnerer ugedagen for en dato som et tal (0-6).
I JavaScript er den første dag i ugen (dag 0) søndag.
Nogle lande i verden anser den første dag i ugen for at være mandag.
const d = new Date("2021-03-25");
d.getDay();
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDay() Method</h2>
<p>Return the weekday as a number:</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getDay();
</script>
</body>
</html>
const d = new Date();
d.getDay();
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDay() Method</h2>
<p>Return the weekday as a number:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getDay();
</script>
</body>
</html>
Du kan bruge en række navne og getDay()
til at returnere ugedag som et navn:
const days = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
const d = new Date("2021-03-25");
let day = days[d.getDay()];
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDay() Method</h2>
<p>Return the weekday as a number.</p>
<p>You can use an array of names to return the weekday as a name:</p>
<p id="demo"></p>
<script>
const days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
const d = new Date("2021-03-25");
let day = days[d.getDay()];
document.getElementById("demo").innerHTML = day;
</script>
</body>
</html>
const days = ["Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"];
const d = new Date();
let day = days[d.getDay()];
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getDay() Method</h2>
<p>Return the weekday as a number.</p>
<p>You can use an array of names to return the weekday as a name:</p>
<p id="demo"></p>
<script>
const days = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
const d = new Date();
let day = days[d.getDay()];
document.getElementById("demo").innerHTML = day;
</script>
</body>
</html>
getTime()
Metoden getTime()
returnerer antallet af millisekunder siden 1. januar 1970:
const d = new Date("1970-01-01");
d.getTime();
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getTime() Method</h2>
<p></p>
<p>Return the number of milliseconds since midnight January 1, 1970:</p>
<p id="demo"></p>
<script>
const d = new Date("1970-01-01");
document.getElementById("demo").innerHTML = d.getTime();
</script>
</body>
</html>
const d = new Date("2021-03-25");
d.getTime();
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getTime() Method</h2>
<p></p>
<p>Return the number of milliseconds since midnight January 1, 1970:</p>
<p id="demo"></p>
<script>
const d = new Date("2021-03-25");
document.getElementById("demo").innerHTML = d.getTime();
</script>
</body>
</html>
const d = new Date();
d.getTime();
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getTime() Method</h2>
<p></p>
<p>Return the number of milliseconds since midnight January 1, 1970:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getTime();
</script>
</body>
</html>
Date.now()
Date.now()
returnerer antallet af millisekunder siden 1. januar 1970.
let ms = Date.now();
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The Date.now() Method</h2>
<p>Return the current date/time in milliseconds since January 1, 1970:</p>
<p id="demo"></p>
<script>
const date = Date.now();
document.getElementById("demo").innerHTML = date;
</script>
</body>
</html>
Beregn antallet af år siden 1970/01/01:
const minute = 1000 * 60;
const hour = minute * 60;
const day = hour * 24;
const year = day * 365;
let years = Math.round(Date.now() / year);
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>Using Date.now()</h2>
<p>Calculate the number of years since January 1, 1970:</p>
<p id="demo"></p>
<script>
// Calculate milliseconds in a year
const minute = 1000 * 60;
const hour = minute * 60;
const day = hour * 24;
const year = day * 365;
// Divide Date.now() with a year
let years = Math.round(Date.now() / year);
document.getElementById("demo").innerHTML = years;
</script>
</body>
</html>
Date.now()
er en statisk metode til Date-objektet.
Du kan ikke bruge det på et datoobjekt som myDate.now()
.
Syntaksen er altid Date.now()
.
Returnerer UTC-datoen
Returnerer UTC-året
Returnerer UTC-måneden
Returnerer UTC-dagen
Returnerer UTC-timen
Returnerer UTC-minutterne
Returnerer UTC sekunder
Returnerer UTC millisekunder
UTC methods use UTC time (Coordinated Universal Time).
UTC time is the same as GMT (Greenwich Mean Time).
The difference between Local time and UTC time can be up to 24 hours.
getTimezoneOffset()
Metoden getTimezoneOffset()
returnerer forskellen (i minutter) mellem lokal tid og UTC tid:
let diff = d.getTimezoneOffset();
Prøv det selv →
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Dates</h1>
<h2>The getTimezoneOffset() Method</h2>
<p>The time zone difference in minutes is:</p>
<p id="demo"></p>
<script>
const d = new Date();
document.getElementById("demo").innerHTML = d.getTimezoneOffset();
</script>
</body>
</html>
For en komplet Datoreference, gå til vores:
Komplet JavaScript-datoreference.
Referencen indeholder beskrivelser og eksempler på alle Dato-egenskaber og metoder.