JavaScript-datometoder


Indholdsfortegnelse

    Vis indholdsfortegnelse


new Date()-konstruktøren

I JavaScript oprettes datoobjekter med new Date().

new Date() returnerer et datoobjekt med den aktuelle dato og klokkeslæt.

Få den aktuelle tid

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>

Dato Hent metoder

getFullYear()

år som et firecifret tal (åååå)

getMonth()

måned som et tal (0-11)

getDate()

dag som et tal (1-31)

getDay()

ugedag som et tal (0-6)

getHours()

time (0-23)

getMinutes()

minut (0-59)

getSeconds()

sekund (0-59)

getMilliseconds()

millisekunder (0-999)

getTime()

tid (millisekunder siden 1. januar 1970)

Note 1

Get-metoderne ovenfor returnerer Lokal tid.

Universal tid (UTC) er dokumenteret nederst på denne side.

Note 2

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.


Metoden getFullYear()

Metoden getFullYear() returnerer året for en dato som et firecifret tal:

Eksempler

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>

Advarsel!

Gammel JavaScript-kode bruger muligvis ikke-standardmetoden getYear().

getYear() formodes at returnere et 2-cifret årstal.

getYear() er forældet. Brug det ikke!


Metoden getMonth()

Metoden getMonth() returnerer måneden for en dato som et tal (0-11).

Bemærk

I JavaScript er januar måned nummer 0, februar er nummer 1, ...

Endelig er december måned nummer 11.

Eksempler

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>

Bemærk

Du kan bruge en række navne til at returnere måneden som et navn:

Eksempler

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>

Metoden getDate()

Metoden getDate() returnerer dagen for en dato som et tal (1-31):

Eksempler

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>


Metoden getHours()

Metoden getHours() returnerer timerne på en dato som et tal (0-23):

Eksempler

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>

Metoden getMinutes()

Metoden getMinutes() returnerer minutterne for en dato som et tal (0-59):

Eksempler

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>

Metoden getSeconds()

Metoden getSeconds() returnerer sekunderne af en dato som et tal (0-59):

Eksempler

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>

Metoden getMilliseconds()

Metoden getMilliseconds() returnerer millisekunderne af en dato som et tal (0-999):

Eksempler

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>

Metoden getDay()

Metoden getDay() returnerer ugedagen for en dato som et tal (0-6).

Bemærk

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.

Eksempler

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>

Bemærk

Du kan bruge en række navne og getDay() til at returnere ugedag som et navn:

Eksempler

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>

Metoden getTime()

Metoden getTime() returnerer antallet af millisekunder siden 1. januar 1970:

Eksempler

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>

Metoden Date.now()

Date.now() returnerer antallet af millisekunder siden 1. januar 1970.

Eksempler

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().


UTC Dato Hent metoder

getUTCDate() / getDate()

Returnerer UTC-datoen

getUTCFullYear() / getFullYear()

Returnerer UTC-året

getUTCMonth() / getMonth()

Returnerer UTC-måneden

getUTCDay() / getDay()

Returnerer UTC-dagen

getUTCHours() / getHours()

Returnerer UTC-timen

getUTCMinutes() / getMinutes()

Returnerer UTC-minutterne

getUTCSeconds() / getSeconds()

Returnerer UTC sekunder

getUTCMilliseconds() / getMilliseconds()

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.






Metoden getTimezoneOffset()

Metoden getTimezoneOffset() returnerer forskellen (i minutter) mellem lokal tid og UTC tid:

Eksempel

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>

Komplet JavaScript-datoreference

For en komplet Datoreference, gå til vores:

Komplet JavaScript-datoreference.

Referencen indeholder beskrivelser og eksempler på alle Dato-egenskaber og metoder.