Obiekt Date

Metoda date.toString()
Metoda data.toDateString()
Metoda data.toTimeString()

<script>
var date = new Date();
document.write("<p>" + date.toString() + "</p>");
document.write("<p>" + date.toDateString() + "</p>");
document.write("<p>" + date.toTimeString() + "</p>");
</script>

Sat Apr 05 2025 16:41:08 GMT+0000 (Coordinated Universal Time)

Sat Apr 05 2025

16:41:08 GMT+0000 (Coordinated Universal Time)

Metoda date.getTimezoneOffset()

<script>
document.write("<p>Różnica między czasem UTC i czasem lokalnym w minutach: " + date.getTimezoneOffset() + "</p>");
</script>

Różnica między czasem UTC i czasem lokalnym w minutach: 0

Metoda date.toUTCString()
Metoda date.toISOString()

<script>
document.write("<p>" + date.toUTCString() + "</p>");
document.write("<p>" + date.toISOString() + "</p>");
</script>

Sat, 05 Apr 2025 16:41:08 GMT

2025-04-05T16:41:08.712Z

Metoda date.toLocaleString()
Metoda date.toLocaleDateString()
Metoda date.toLocaleTimeString()

<script>
document.write("<p>" + date.toLocaleString() + "</p>");
document.write("<p>" + date.toLocaleDateString() + "</p>");
document.write("<p>" + date.toLocaleTimeString() + "</p>");
</script>

4/5/2025, 4:41:08 PM

4/5/2025

4:41:08 PM

Metoda date.getDate()
Metoda date.getMonth()
Metoda date.getFullyear()

<script>
var today = date.getDate() + "." + date.getMonth() + "." + date.getFullYear();
document.write("<p>" + today + "</p>");
</script>

5.3.2025

Metoda date.getHours()
Metoda date.getMinutes()
Metoda date.getSeconds()

<script>
var add0 = x => x < 10 ? "0" + x : x; 
var time = date.getHours() + ":" + add0(date.getMinutes()) + ":" + add0(date.getSeconds());
document.write("<p>" + time + "</p>");
</script>

16:41:08

Metoda date.getUTCHours()
Metoda date.getUTCMinutes()
Metoda date.getUTCSeconds()

<script>
var time = date.getUTCHours() + ":" + add0(date.getUTCMinutes()) + ":" + add0(date.getUTCSeconds());
document.write("<p>" + time + "</p>");
</script>

16:41:08

Metoda date.setDate(day)
Metoda date.setMonth(month, day)
Metoda date.setFullYear(year, month, day)

<script>
var date2 = new Date();
date2.setDate(7);
date2.setMonth(2);
date2.setFullYear(1975);
document.write("<p>" + date2 + "</p>");
</script>

Fri Mar 07 1975 16:41:08 GMT+0000 (Coordinated Universal Time)

Metoda date.getDay()

<script>
var dni = ["Niedziela","Poniedziałek","Wtorek","Środa","Czwartek","Piątek","Sobota"];
document.write("<p>Dzień tygodnia: " + dni[date.getDay()] + "</p>");
</script>

Dzień tygodnia: Sobota

Przykład 1

<script>
var days = ["Niedziela","Poniedziałek","Wtorek","Środa","Czwartek","Piątek","Sobota"];
var months = ["Styczeń","Luty","Marzec","Kwiecień","Maj","Czerwiec",
              "Lipiec","Sierpień","Wrzesień","Październik","Listopad","Grudzień"];
var data = days[date.getDay()] + " " + date.getDate() + ", " + months[date.getMonth()] + " " + date.getFullYear();
document.write("<p>" + data + "</p>");
</script>

Sobota 5, Kwiecień 2025

Przykład 2

<body onload="start()" onunload="stop()">

<script>
var interval = null;
function start()
{
  interval = setInterval(setTime, 1000);
}
function stop()
{
  clearInterval(interval);
}
function setTime() {
  var date = new Date();
  var add0 = x => x < 10 ? "0" + x : x;
  var time = date.getHours() + ":" + add0(date.getMinutes()) + ":" + add0(date.getSeconds());
  document.getElementById("clock").value = time;
}
</script>

<input type="text" id="clock" size="10">

Strona główna